Android BLE蓝牙设置为从机模式的demo
时间: 2024-03-06 17:49:38 浏览: 131
以下是一个简单的 Android BLE 蓝牙从机模式示例,可以帮助您入门:
首先,您需要在 AndroidManifest.xml 文件中添加蓝牙权限:
```
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-feature android:name="android.hardware.bluetooth_le" android:required="true" />
```
然后,在您的 Activity 中,您需要实例化 BluetoothGattServer 对象,创建一个服务和特征,并将其添加到 BluetoothGattServer 中。以下是一个简单的示例:
```
private BluetoothManager mBluetoothManager;
private BluetoothGattServer mGattServer;
private BluetoothGattService mGattService;
private BluetoothGattCharacteristic mGattCharacteristic;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 初始化蓝牙管理器
mBluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
// 创建从机服务
mGattService = new BluetoothGattService(UUID.fromString("0000180d-0000-1000-8000-00805f9b34fb"),
BluetoothGattService.SERVICE_TYPE_PRIMARY);
// 创建从机特征
mGattCharacteristic = new BluetoothGattCharacteristic(UUID.fromString("00002a37-0000-1000-8000-00805f9b34fb"),
BluetoothGattCharacteristic.PROPERTY_READ | BluetoothGattCharacteristic.PROPERTY_NOTIFY,
BluetoothGattCharacteristic.PERMISSION_READ);
// 将特征添加到服务中
mGattService.addCharacteristic(mGattCharacteristic);
// 创建蓝牙从机
mGattServer = mBluetoothManager.openGattServer(this, mGattServerCallback);
// 将服务添加到从机
mGattServer.addService(mGattService);
}
```
接下来,您需要实现 BluetoothGattServerCallback 回调函数,以处理来自中央设备的请求和通知。以下是一个简单的示例:
```
private BluetoothGattServerCallback mGattServerCallback = new BluetoothGattServerCallback() {
@Override
public void onConnectionStateChange(BluetoothDevice device, int status, int newState) {
super.onConnectionStateChange(device, status, newState);
// 处理连接状态变化事件
}
@Override
public void onCharacteristicReadRequest(BluetoothDevice device, int requestId, int offset, BluetoothGattCharacteristic characteristic) {
super.onCharacteristicReadRequest(device, requestId, offset, characteristic);
// 处理读取请求
mGattServer.sendResponse(device, requestId, BluetoothGatt.GATT_SUCCESS, offset, characteristic.getValue());
}
@Override
public void onCharacteristicWriteRequest(BluetoothDevice device, int requestId, BluetoothGattCharacteristic characteristic, boolean preparedWrite, boolean responseNeeded, int offset, byte[] value) {
super.onCharacteristicWriteRequest(device, requestId, characteristic, preparedWrite, responseNeeded, offset, value);
// 处理写入请求
characteristic.setValue(value);
mGattServer.sendResponse(device, requestId, BluetoothGatt.GATT_SUCCESS, offset, null);
}
@Override
public void onNotificationSent(BluetoothDevice device, int status) {
super.onNotificationSent(device, status);
// 处理通知发送事件
}
};
```
最后,您需要开始广告您的 BLE 从机服务,以便中央设备可以发现和连接到它。以下是一个简单的示例:
```
private BluetoothLeAdvertiser mBluetoothLeAdvertiser;
private void startAdvertising() {
mBluetoothLeAdvertiser = BluetoothAdapter.getDefaultAdapter().getBluetoothLeAdvertiser();
AdvertiseSettings settings = new AdvertiseSettings.Builder()
.setAdvertiseMode(AdvertiseSettings.ADVERTISE_MODE_LOW_LATENCY)
.setConnectable(true)
.setTimeout(0)
.setTxPowerLevel(AdvertiseSettings.ADVERTISE_TX_POWER_HIGH)
.build();
AdvertiseData data = new AdvertiseData.Builder()
.setIncludeDeviceName(true)
.setIncludeTxPowerLevel(false)
.addServiceUuid(new ParcelUuid(UUID.fromString("0000180d-0000-1000-8000-00805f9b34fb")))
.build();
mBluetoothLeAdvertiser.startAdvertising(settings, data, mAdvertiseCallback);
}
private AdvertiseCallback mAdvertiseCallback = new AdvertiseCallback() {
@Override
public void onStartSuccess(AdvertiseSettings settingsInEffect) {
super.onStartSuccess(settingsInEffect);
// 处理广告启动成功事件
}
@Override
public void onStartFailure(int errorCode) {
super.onStartFailure(errorCode);
// 处理广告启动失败事件
}
};
```
这就是一个简单的 Android BLE 蓝牙从机模式示例,希望可以帮助您入门。
阅读全文