Android BLE learning (3): write your own BLE bluetooth reading and writing tool (the function is modeled on the nrf master control panel)

http://my.csdn.net/lidec


background

Since Nordic’s official nrf master control panel only provides apk, many students have to explore Bluetooth reading and writing when they learn. The basic reading and writing methods of the BLE module and some commonly used UUIDs are organized in the project, and some Bluetooth operations are extracted. The process is convenient for the  development of Android app code. I hope to help students who are learning BLE Bluetooth.

The previous article summarized the connection of the 51822 BLE Bluetooth module and some knowledge about the Bluetooth protocol. This article will integrate the foregoing content and refer to the official nrf master control panel to realize the search, connection, and related reading and writing of Android phones and Bluetooth modules. .

Project address: https://git.oschina.net/vonchenchen/BLE_Assitant.git 
apk download: http://download.csdn.net/detail/lidec/9468739

Process

Activity communicates with Bluetooth service

ActivityActivityBLEControlServiceBLEControlService Bluetooth function service, data changes send corresponding broadcast BLEStatusChangeReceiver monitoring, callback OnBLEStatusChangeListener transfer data to Activity

Monitor the changes of the Bluetooth service and send a broadcast to BLEStatusChangeReceiver after the change

BLEControlServiceBLEControlServiceMyBluetoothGattCallbackMyBluetoothGattCallbackgatt data change callback to send broadcast to BLEStatusChangeReceiver

Get the data after Bluetooth changes through the callback function, so that you can flexibly display different types in different places. This is more convenient to imitate the official master's Bluetooth log function, while taking into account the display of data in other controls, enhancing the flexibility of the program.

When Bluetooth writing and reading and writing feature values ​​and descriptor data or other data, corresponding methods need to be provided in BLEControlService, and the underlying implementation needs to call the methods provided in mBluetoothGatt. See the data provided by mBluetoothGatt for specific functions. Here we provide the following example methods

public boolean connect(final String address)  蓝牙连接

public void disconnect()                      蓝牙断开

public void close()                           蓝牙关闭

public void readCharacteristic(BluetoothGattCharacteristic characteristic)    读取readCharacteristic的信息

public void writeCharacteristic(BluetoothGattCharacteristic characteristic, byte[] value)  写入readCharacteristic的信息

public void readDiscriptor(BluetoothGattDescriptor descriptor) 读取描述符信息

public void readRemoteRssi()    读取ble的rssi

public void enableXXXXXNotification()  将某Characteristic设置为Notification


 
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

When we receive data, we will trigger the method in MyBluetoothGattCallback to get the received data.

Project effect

Write picture description here 
BLE device list

Write picture description here 
Data change list

Write picture description here

Write picture description here 
BLE details read and write

Write picture description here


Guess you like

Origin blog.csdn.net/lilifang_2011/article/details/72897370