安卓应用程序是否需要同时扮演中心设备和外围设备的角色才能通过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文件中添加相应的权限和特性声明,以及在运行时请求蓝牙权限。

相关内容

热门资讯

2分钟辅助!创思维激k辅助,其... 2分钟辅助!创思维激k辅助,其实是真的有辅助器(有挂攻略)1、玩家可以在创思维激k辅助线上大神俱乐部...
第2分钟辅助!智星菠萝透视,一... 第2分钟辅助!智星菠萝透视,一直是有辅助技巧(有挂总结)所有人都在同一条线上,像星星一样排成一排,每...
八分钟辅助!广东雀神胜率修改,... 八分钟辅助!广东雀神胜率修改,确实有辅助插件(存在有挂)1、点击下载安装,广东雀神胜率修改脚本分类一...
第1分钟辅助!雀友软件脚本辅助... 第1分钟辅助!雀友软件脚本辅助器,真是是有辅助脚本(有挂解密)1、在雀友软件脚本辅助器插件功能辅助器...
第4分钟辅助!海盗来了辅助器最... 第4分钟辅助!海盗来了辅助器最新,其实是有辅助器(有挂规律)海盗来了辅助器最新破解侠是真的助透视。每...
七分钟辅助!皮皮辅助,好像真的... 七分钟辅助!皮皮辅助,好像真的有辅助插件(有挂细节)在进入皮皮辅助软件靠谱后,参与本局比赛的八名玩家...
四分钟辅助!开心泉州免费辅助器... 四分钟辅助!开心泉州免费辅助器,本来是真的有辅助神器(有挂分享)1、开心泉州免费辅助器辅助器安装包、...
第8分钟辅助!欢聚水鱼虎步龙行... 第8分钟辅助!欢聚水鱼虎步龙行辅助器,本来有辅助神器(有挂方式)在进入欢聚水鱼虎步龙行辅助器软件靠谱...
第三分钟辅助!奇迹手游辅助,其... 第三分钟辅助!奇迹手游辅助,其实真的有辅助攻略(有挂细节)1、奇迹手游辅助有没有辅助教程、奇迹手游辅...
七分钟辅助!闲逸辅助插件,总是... 七分钟辅助!闲逸辅助插件,总是是真的有辅助神器(有挂方法)1、全新机制【闲逸辅助插件ai辅助工具激活...