Effect of 1.
1.2 READ/NOTIFY/INDICATE/WRITE after connection
2. Use
Bleadmin. Java, Bluetooth device control class, used to do bluetooth device management, scanning, etc
Bleoperator. Java corresponds to a Bluetooth device and manages operations such as READ, INDICATE, NOTIFY, WRITE, CONNECT, and DISCONNECT on the device
BleDeviceService. Java is used to describe the bluetooth READ, INDICATE, NOTIFY, WRITE the function of the four classes
The classes below callback mainly control some callbacks
2.2 the use of
/ / mDeviceCallBack is bluetooth connection status callback / / mDegviceOperationCallback is bluetooth operation callback, the WRITE, MBleAdmin = new BleAdmin(this, mDeviceCallBack, mDeviceOperationCallback);
Copy the code
2.2.2 SCAN
// Enable bluetooth mbleadmin.openble (); // Start the search, Set the search callback mBleAdmin. StartScanAllDevice (new ScanCallback () {@ Override public void onDeviceFound (final List < BluetoothDevice > devices) { Log.e("Found", " " + devices.size()); RunOnUiThread (new Runnable () {@ Override public void the run () {/ / display device to the main thread mRecyclerAdapter. SetDevices (devices); }}); }});
Copy the code
viewHolder.content.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mCurrentDeviceAddress = device.getAddress();
//mBleAdmin.connectDevice(device.getAddress()); Connect directly with the address
mBleAdmin.connectDevice(device);//Direct connection with the searched device is recommended}});Copy the code
2.2.4 the Read
//Enable READ operation
mBleAdmin.processDeviceService(new BleDeviceService(
mCurrentDeviceAddress, //Connected address of bluetooth to enable read operation
characteristic.getUuid(), //The eigenvalue of the read operation
BleDeviceService.OperateType.Read));//Set OperationType to READ
Copy the code
//Enabling Notify
mBleAdmin.processDeviceService(new BleDeviceService(
mCurrentDeviceAddress, //Enable the address of the connected Bluetooth for Notify
characteristic.getUuid(), //The characteristic value of the Notify operation
BleDeviceService.OperateType.Notify));//Set OperationType to NOTIFYCopy the code
2.2.6 Write/Indicate
mWrite.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mBleAdmin.processDeviceService(new BleDeviceService(
mCurrentDeviceAddress,//Address of connected Bluetooth that enables Write
UUID.fromString(SampleGattAttributes.INSOLE_WRITE),//Write UUID characteristic value
BleDeviceService.OperateType.Write.//OperationType
mEditData.getText().toString().getBytes()));//Byte [] value to be written}});Copy the code
mDisconnecte.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//mBleAdmin.disconnectDevice(mCurrentDevce); // Or pass it directly to the device
mBleAdmin.disconnectDevice(mCurrentDeviceAddress);//Directly pass in the address of the connected Bluetooth device}});Copy the code
Reference 3.
FastBle