版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_39238370/article/details/82902789
关于蓝牙方面主要分为几步,这也要感谢上家公司,以前没有了解过蓝牙开发,因为需要和硬件通信,接触到一些,做此总结。
1.获取系统蓝牙Adapter
//获取蓝牙适配器
public BluetoothAdapter getBlueAdapter() {
BluetoothAdapter mBluetoothAdapter;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2) {
BluetoothManager mBluetoothManager = (BluetoothManager) context.getSystemService(Context.BLUETOOTH_SERVICE);
mBluetoothAdapter = mBluetoothManager.getAdapter();
} else {
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
}
return mBluetoothAdapter;
}
2. 开始扫描蓝牙设备 BluetoothAdapter.LeScanCallback
// 开始扫描
public boolean startLeScan(final BluetoothAdapter mBluetoothAdapter, BluetoothAdapter.LeScanCallback mLeScanCallback) {
boolean isStart = mBluetoothAdapter.startLeScan(mLeScanCallback);
return isStart;
}
//扫描的蓝牙
private BluetoothAdapter.LeScanCallback getmLeScanCallback = new BluetoothAdapter.LeScanCallback() {
@Override
public void onLeScan(BluetoothDevice device, int i, byte[] bytes) {
device.getName();//蓝牙名称
device.getAddress();//蓝牙地址 建立连接
}
};
3. 连接蓝牙建立Gatt连接
private void connet(BluetoothDevice device) {
if (bluetoothGatt != null) {
connetAddress = device.getAddress();
bluetoothGatt = device.connectGatt(context, false, mGattCallback);
}
}
4. 构建Gatt连接
BluetoothGattCallback gatt = new BluetoothGattCallback() {
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
super.onConnectionStateChange(gatt, status, newState);
if (BluetoothGatt.GATT_SUCCESS == status) {//连接成功
Log.d(TAG, "onConnectionStateChange:status ---> success");
gatt.discoverServices(); // 发现服务(必须要蓝牙先连接成功)
} else if (BluetoothGatt.STATE_DISCONNECTED == status) {//断开连接
Log.d(TAG, "onConnectionStateChange:status ---> disconnected ");
BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(connetAddress);
}
}
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
super.onServicesDiscovered(gatt, status);
Log.d(TAG, "onServicesDiscovered ---> 找到服务" + status);
if (gatt.GATT_SUCCESS == status) {
bluetoothGattService = bluetoothGatt.getService(UUID.fromString(service_uuid)); //获取服务的uuid
writeCharacteristic = bluetoothGattService.getCharacteristic(UUID.fromString(write_uuid)); //获取写的uuid
BluetoothGattCharacteristic notificationCharacteristic = bluetoothGattService.getCharacteristic(UUID.fromString(notify_uuid));
enableNotification(true, gatt, notificationCharacteristic);//用于获取读写回调的通知,必须要有,否则接收不到数据*/
}
}
private void enableNotification(boolean enable, BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
if (gatt == null || characteristic == null) return; //这一步必须要有 否则收不到通知
Log.d(TAG, "characters_uuid ---> " + characters_uuid);
BluetoothGattDescriptor characteristicDescriptor = characteristic.getDescriptor(UUID.fromString(characters_uuid));
if (characteristicDescriptor != null) {
byte[] value = enable ? BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE : BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE;
characteristicDescriptor.setValue(value);
gatt.writeDescriptor(characteristicDescriptor);
}
gatt.setCharacteristicNotification(characteristic, enable);
}
@Override
public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
super.onCharacteristicWrite(gatt, characteristic, status);
//写入数据成功会回调,注意每次只能读取20字节。
}
@Override
public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
super.onCharacteristicChanged(gatt, characteristic);
//读取成功会回调,注意每次只能20字节,大于20字节需要自己拼包。
}
};
这样就可以简单的与蓝牙通信,如有不足请指出,及时回复。