从Android5.0开始才支持android
如下是关键BLE术语和概念的摘要:git
如下是Android设备与BLE设备互动时适用的角色和职责:github
GATT服务器与GATT客户端。 这决定了两个设备在创建链接后如何相互通讯。服务器
首先,须要在manifest中声明使用蓝牙和操做蓝牙的权限app
在应用程序清单文件中声明蓝牙权限。 例如:
若是您要声明本身的应用只适用于支持BLE的设备,请在应用清单中包含如下内容:优化
<uses-feature android:name =“android.hardware.bluetooth_le”android:required =“true”/>
不过,若是您想让应用程式适用于不支援BLE的装置,您仍应在应用的清单中加入这个元素,但required="false"设为required="false" 。
而后在运行时,您能够经过使用PackageManager.hasSystemFeature()肯定BLE可用性:ui
// Use this check to determine whether BLE is supported on the device. Then // you can selectively disable BLE-related features. if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) { Toast.makeText(this, R.string.ble_not_supported, Toast.LENGTH_SHORT).show(); finish(); }
在android 6.0 之后,要想得到蓝牙扫描结果,还须要下面的权限this
<manifest ... > <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> ... <!-- Needed only if your app targets Android 5.0 (API level 21) or higher. --> <uses-feature android:name="android.hardware.location.gps" /> ... </manifest>
得到蓝牙适配器日志
private BluetoothAdapter mBluetoothAdapter; ... // Initializes Bluetooth adapter. final BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE); mBluetoothAdapter = bluetoothManager.getAdapter();
打开蓝牙
// Ensures Bluetooth is available on the device and it is enabled. If not, // displays a dialog requesting user permission to enable Bluetooth. if (mBluetoothAdapter == null || !mBluetoothAdapter.isEnabled()) { Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT); }
(1)广播的设置
(2)设置广播的数据
(3)设置响应的数据
(4)设置链接回调
private void initGATTServer() { AdvertiseSettings settings = new AdvertiseSettings.Builder() .setConnectable(true) .build(); AdvertiseData advertiseData = new AdvertiseData.Builder() .setIncludeDeviceName(true) .setIncludeTxPowerLevel(true) .build(); AdvertiseData scanResponseData = new AdvertiseData.Builder() .addServiceUuid(new ParcelUuid(UUID_SERVER)) .setIncludeTxPowerLevel(true) .build(); AdvertiseCallback callback = new AdvertiseCallback() { @Override public void onStartSuccess(AdvertiseSettings settingsInEffect) { Log.d(TAG, "BLE advertisement added successfully"); showText("1. initGATTServer success"); println("1. initGATTServer success"); initServices(getContext()); } @Override public void onStartFailure(int errorCode) { Log.e(TAG, "Failed to add BLE advertisement, reason: " + errorCode); showText("1. initGATTServer failure"); } }; BluetoothLeAdvertiser bluetoothLeAdvertiser = mBluetoothAdapter.getBluetoothLeAdvertiser(); bluetoothLeAdvertiser.startAdvertising(settings, advertiseData, scanResponseData, callback); }
在被BLE设备链接后,将触发 AdvertiseCallback 的 onStartSuccess,咱们在这以后,初始化GATT的服务
(1) 经过 mBluetoothManager.openGattServer() 得到 bluetoothGattServer
(2) 添加 服务,特征,描述。这些内容要让客户端知道。
private void initServices(Context context) { bluetoothGattServer = mBluetoothManager.openGattServer(context, bluetoothGattServerCallback); BluetoothGattService service = new BluetoothGattService(UUID_SERVER, BluetoothGattService.SERVICE_TYPE_PRIMARY); //add a read characteristic. characteristicRead = new BluetoothGattCharacteristic(UUID_CHARREAD, BluetoothGattCharacteristic.PROPERTY_READ, BluetoothGattCharacteristic.PERMISSION_READ); //add a descriptor BluetoothGattDescriptor descriptor = new BluetoothGattDescriptor(UUID_DESCRIPTOR, BluetoothGattCharacteristic.PERMISSION_WRITE); characteristicRead.addDescriptor(descriptor); service.addCharacteristic(characteristicRead); //add a write characteristic. BluetoothGattCharacteristic characteristicWrite = new BluetoothGattCharacteristic(UUID_CHARWRITE, BluetoothGattCharacteristic.PROPERTY_WRITE | BluetoothGattCharacteristic.PROPERTY_READ | BluetoothGattCharacteristic.PROPERTY_NOTIFY, BluetoothGattCharacteristic.PERMISSION_WRITE); service.addCharacteristic(characteristicWrite); bluetoothGattServer.addService(service); Log.e(TAG, "2. initServices ok"); showText("2. initServices ok"); }
在 openGattServer 方法中,咱们须要传入个回调
bluetoothGattServer = mBluetoothManager.openGattServer(context, bluetoothGattServerCallback);
回调时间有:链接状态变化,收发消息,通知消息
/** * 服务事件的回调 */ private BluetoothGattServerCallback bluetoothGattServerCallback = new BluetoothGattServerCallback() { /** * 1.链接状态发生变化时 * @param device * @param status * @param newState */ @Override public void onConnectionStateChange(BluetoothDevice device, int status, int newState) { Log.e(TAG, String.format("1.onConnectionStateChange:device name = %s, address = %s", device.getName(), device.getAddress())); Log.e(TAG, String.format("1.onConnectionStateChange:status = %s, newState =%s ", status, newState)); super.onConnectionStateChange(device, status, newState); } @Override public void onServiceAdded(int status, BluetoothGattService service) { super.onServiceAdded(status, service); Log.e(TAG, String.format("onServiceAdded:status = %s", status)); } @Override public void onCharacteristicReadRequest(BluetoothDevice device, int requestId, int offset, BluetoothGattCharacteristic characteristic) { Log.e(TAG, String.format("onCharacteristicReadRequest:device name = %s, address = %s", device.getName(), device.getAddress())); Log.e(TAG, String.format("onCharacteristicReadRequest:requestId = %s, offset = %s", requestId, offset)); bluetoothGattServer.sendResponse(device, requestId, BluetoothGatt.GATT_SUCCESS, offset, characteristic.getValue()); // super.onCharacteristicReadRequest(device, requestId, offset, characteristic); } /** * 3. onCharacteristicWriteRequest,接收具体的字节 * @param device * @param requestId * @param characteristic * @param preparedWrite * @param responseNeeded * @param offset * @param requestBytes */ @Override public void onCharacteristicWriteRequest(BluetoothDevice device, int requestId, BluetoothGattCharacteristic characteristic, boolean preparedWrite, boolean responseNeeded, int offset, byte[] requestBytes) { Log.e(TAG, String.format("3.onCharacteristicWriteRequest:device name = %s, address = %s", device.getName(), device.getAddress())); Log.e(TAG, String.format("3.onCharacteristicWriteRequest:requestId = %s, preparedWrite=%s, responseNeeded=%s, offset=%s, value=%s", requestId, preparedWrite, responseNeeded, offset, OutputStringUtil.toHexString(requestBytes))); bluetoothGattServer.sendResponse(device, requestId, BluetoothGatt.GATT_SUCCESS, offset, requestBytes); //4.处理响应内容 onResponseToClient(requestBytes, device, requestId, characteristic); } /** * 2.描述被写入时,在这里执行 bluetoothGattServer.sendResponse(device, requestId, BluetoothGatt.GATT_SUCCESS... 收,触发 onCharacteristicWriteRequest * @param device * @param requestId * @param descriptor * @param preparedWrite * @param responseNeeded * @param offset * @param value */ @Override public void onDescriptorWriteRequest(BluetoothDevice device, int requestId, BluetoothGattDescriptor descriptor, boolean preparedWrite, boolean responseNeeded, int offset, byte[] value) { Log.e(TAG, String.format("2.onDescriptorWriteRequest:device name = %s, address = %s", device.getName(), device.getAddress())); Log.e(TAG, String.format("2.onDescriptorWriteRequest:requestId = %s, preparedWrite = %s, responseNeeded = %s, offset = %s, value = %s,", requestId, preparedWrite, responseNeeded, offset, OutputStringUtil.toHexString(value))); // now tell the connected device that this was all successfull bluetoothGattServer.sendResponse(device, requestId, BluetoothGatt.GATT_SUCCESS, offset, value); } /** * 5.特征被读取。当回复响应成功后,客户端会读取而后触发本方法 * @param device * @param requestId * @param offset * @param descriptor */ @Override public void onDescriptorReadRequest(BluetoothDevice device, int requestId, int offset, BluetoothGattDescriptor descriptor) { Log.e(TAG, String.format("onDescriptorReadRequest:device name = %s, address = %s", device.getName(), device.getAddress())); Log.e(TAG, String.format("onDescriptorReadRequest:requestId = %s", requestId)); // super.onDescriptorReadRequest(device, requestId, offset, descriptor); bluetoothGattServer.sendResponse(device, requestId, BluetoothGatt.GATT_SUCCESS, offset, null); } @Override public void onNotificationSent(BluetoothDevice device, int status) { super.onNotificationSent(device, status); Log.e(TAG, String.format("5.onNotificationSent:device name = %s, address = %s", device.getName(), device.getAddress())); Log.e(TAG, String.format("5.onNotificationSent:status = %s", status)); } @Override public void onMtuChanged(BluetoothDevice device, int mtu) { super.onMtuChanged(device, mtu); Log.e(TAG, String.format("onMtuChanged:mtu = %s", mtu)); } @Override public void onExecuteWrite(BluetoothDevice device, int requestId, boolean execute) { super.onExecuteWrite(device, requestId, execute); Log.e(TAG, String.format("onExecuteWrite:requestId = %s", requestId)); } };
调用 bluetoothGattServer.notifyCharacteristicChanged 方法,通知数据改变。
/** * 4.处理响应内容 * * @param reqeustBytes * @param device * @param requestId * @param characteristic */ private void onResponseToClient(byte[] reqeustBytes, BluetoothDevice device, int requestId, BluetoothGattCharacteristic characteristic) { Log.e(TAG, String.format("4.onResponseToClient:device name = %s, address = %s", device.getName(), device.getAddress())); Log.e(TAG, String.format("4.onResponseToClient:requestId = %s", requestId)); String msg = OutputStringUtil.transferForPrint(reqeustBytes); println("4.收到:" + msg); showText("4.收到:" + msg); String str = new String(reqeustBytes) + " hello>"; characteristicRead.setValue(str.getBytes()); bluetoothGattServer.notifyCharacteristicChanged(device, characteristicRead, false); println("4.响应:" + str); showText("4.响应:" + str); }
(1) 当客户端开始写入数据时: 触发回调方法 onDescriptorWriteRequest
(2) 在 onDescriptorWriteRequest 方法中,执行下面的方法表示 写入成功 BluetoothGatt.GATT_SUCCESS
bluetoothGattServer.sendResponse(device, requestId, BluetoothGatt.GATT_SUCCESS, offset, value); 执行 sendResponse后,会触发回调方法 onCharacteristicWriteRequest
(3) 在 onCharacteristicWriteRequest方法中
public void onCharacteristicWriteRequest(BluetoothDevice device, int requestId, BluetoothGattCharacteristic characteristic, boolean preparedWrite, boolean responseNeeded, int offset, byte[] requestBytes) { 这个里能够得到 来自客户端发来的数据 requestBytes
(4) 处理响应内容,我写了这个方法:
onResponseToClient(requestBytes, device, requestId, characteristic); 在这个方法中,经过 bluetoothGattServer.notifyCharacteristicChanged()方法 回复数据
1.onConnectionStateChange:device name = null, address = 74:32:DE:49:3C:28 1.onConnectionStateChange:status = 0, newState =2 2.onDescriptorWriteRequest:device name = null, address = 74:32:DE:49:3C:28 2.onDescriptorWriteRequest:requestId = 1, preparedWrite = false, responseNeeded = true, offset = 0, value = [01,00,], 3.onCharacteristicWriteRequest:device name = null, address = 74:32:DE:49:3C:28 3.onCharacteristicWriteRequest:requestId = 2, preparedWrite=false, responseNeeded=false, offset=0, value=[41,54,45,30,0D,] 4.onResponseToClient:device name = null, address = 74:32:DE:49:3C:28 4.onResponseToClient:requestId = 2 4.收到:ATE0 4.响应:ATE0 hello> 5.onNotificationSent:device name = null, address = 74:32:DE:49:3C:28 5.onNotificationSent:status = 0
代码托管到github:
https://github.com/vir56k/bluetoothDemo 找到 bleperipheraldemo 文件夹