关于蓝牙通信
首先要知道几个类,BluetoothAdapter,BluetoothGatt,BluetoothDevice,BluetoothCattService,BluetoothCattCharacteristic。
第一个是蓝牙设配器,对蓝牙的操作都需要用到它,很重要,BluetoothGatt作为中央来使用和处理数据,使用时有一个回调方法BluetoothGattCallback返回中央的状态和周边提供的数据,BluetoothCattService作为周边来提供数据;BluetoothGattServerCallback返回周边的状态。BluetoothDevice是蓝牙设备,BluetoothCattCharacteristic是蓝牙设备的特征。
下面正是开始
1.加上蓝牙访问权限
2.打开蓝牙,两种方式,一种是询问式,一种是自动式,你百度一下就有很多,很简单,这里就不多说。
3.搜索周边的蓝牙设备
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
adapter.startDiscovery();//此过程会发送广播信息,我们在BroadcastReceiver中去接收即可