安卓应用程序是否需要同时扮演中心设备和外围设备的角色才能通过BLE发送和接收数据?
创始人
2024-11-09 18:00:43
0次

在安卓应用程序中,通常情况下并不需要同时扮演中心设备和外围设备的角色来通过BLE(低功耗蓝牙)发送和接收数据。通常情况下,安卓设备可以作为中心设备与外围设备通信,或者作为外围设备与中心设备通信。

以下是一个简单的代码示例,演示如何在安卓设备上作为中心设备与外围设备进行BLE通信:

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCallback;
import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothGattService;
import android.bluetooth.BluetoothManager;
import android.content.Context;

public class BleManager {
    private Context context;
    private BluetoothAdapter bluetoothAdapter;
    private BluetoothGatt bluetoothGatt;

    public BleManager(Context context) {
        this.context = context;
        BluetoothManager bluetoothManager = (BluetoothManager) context.getSystemService(Context.BLUETOOTH_SERVICE);
        bluetoothAdapter = bluetoothManager.getAdapter();
    }

    public void connectToDevice(BluetoothDevice device) {
        bluetoothGatt = device.connectGatt(context, false, gattCallback);
    }

    public void disconnect() {
        if (bluetoothGatt != null) {
            bluetoothGatt.disconnect();
            bluetoothGatt.close();
        }
    }

    private BluetoothGattCallback gattCallback = new BluetoothGattCallback() {
        @Override
        public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
            super.onConnectionStateChange(gatt, status, newState);
            if (newState == BluetoothGatt.STATE_CONNECTED) {
                // 连接成功
                gatt.discoverServices();
            } else if (newState == BluetoothGatt.STATE_DISCONNECTED) {
                // 连接断开
            }
        }

        @Override
        public void onServicesDiscovered(BluetoothGatt gatt, int status) {
            super.onServicesDiscovered(gatt, status);
            if (status == BluetoothGatt.GATT_SUCCESS) {
                BluetoothGattService service = gatt.getService(serviceUUID);
                BluetoothGattCharacteristic characteristic = service.getCharacteristic(characteristicUUID);
                // 读取特征值
                gatt.readCharacteristic(characteristic);
                // 发送数据到外围设备
                characteristic.setValue(data);
                gatt.writeCharacteristic(characteristic);
            }
        }

        @Override
        public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
            super.onCharacteristicRead(gatt, characteristic, status);
            if (status == BluetoothGatt.GATT_SUCCESS) {
                // 读取特征值成功
                byte[] data = characteristic.getValue();
                // 处理接收到的数据
            }
        }

        @Override
        public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
            super.onCharacteristicWrite(gatt, characteristic, status);
            if (status == BluetoothGatt.GATT_SUCCESS) {
                // 发送数据成功
            }
        }
    };
}

在使用上述代码示例时,首先需要获取蓝牙适配器(BluetoothAdapter)并连接到外围设备。然后,在连接状态改变回调(onConnectionStateChange)中调用discoverServices()以发现外围设备提供的服务。在服务发现回调(onServicesDiscovered)中,可以获取特定服务和特征值,并使用readCharacteristic()读取特征值,或者使用writeCharacteristic()发送数据到外围设备。在读取特征值回调(onCharacteristicRead)和写入特征值回调(onCharacteristicWrite)中,可以处理读取和写入操作的结果。

需要注意的是,具体的UUID和数据格式等需要根据实际情况进行配置和处理。此外,还需要在AndroidManifest.xml文件中添加相应的权限和特性声明,以及在运行时请求蓝牙权限。

相关内容

热门资讯

热点推荐开挂!利发娱乐辅助透视... 热点推荐开挂!利发娱乐辅助透视挂,雀友会潮汕麻将存在有外挂,本来有挂秘诀运利发娱乐辅助工具,进入游戏...
此事引发网友热议透视挂!约约吉... 此事引发网友热议透视挂!约约吉林麻将辅助透视挂,无忧是有外挂,总是有挂教学1、金币登录送、破产送、升...
重大消息辅助!涡阳茶馆辅助透视... 重大消息辅助!涡阳茶馆辅助透视挂,闲榆娱乐存在有外挂,总是有挂功能涡阳茶馆破解侠是真的助透视。每个模...
分享个大家辅助!TX双城棋牌辅... 分享个大家辅助!TX双城棋牌辅助透视挂,廊坊麻将是有外挂,一直有挂秘籍1)TX双城棋牌辅助插件:进一...
一分钟揭秘开挂!川远互动辅助透... 一分钟揭秘开挂!川远互动辅助透视挂,禾沐确实有外挂,一贯有挂功能1、这是跨平台的川远互动轻量版有透视...
此事迅速冲上热搜辅助!老铁十三... 此事迅速冲上热搜辅助!老铁十三水辅助透视挂,梦黄麻将存在有外挂,其实有挂方法1、老铁十三水免费脚本咨...
一分钟了解开挂!涅槃辅助透视挂... 一分钟了解开挂!涅槃辅助透视挂,友友麻将确实有外挂,好像有挂神器1、进入到友友麻将是否有挂之后,能看...
总算了解透视挂!286com辅... 总算了解透视挂!286com辅助透视挂,星悦福建麻将存在有外挂,好像有挂方针运286com辅助工具,...
重大通报透视挂!酉游辅助透视挂... 重大通报透视挂!酉游辅助透视挂,微乐长沙麻将存在有外挂,都是有挂透视1、酉游透视辅助软件激活码多个强...
推荐攻略透视挂!小乐休闲辅助透... 推荐攻略透视挂!小乐休闲辅助透视挂,大唐河北麻将是真的有外挂,总是有挂方式1、用户打开应用后不用登录...