_安卓蓝牙 gatt 传输文件">
赞
踩
<!-- 蓝牙权限 -->
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<!-- 定位权限 -->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
/**
* 申请运行时权限,不授予会搜索不到设备
*/
private void initPermissions() {
if (ContextCompat.checkSelfPermission(this, "android.permission-group.LOCATION") != 0) {
ActivityCompat.requestPermissions(
this,
new String[]{
"android.permission.ACCESS_FINE_LOCATION",
"android.permission.ACCESS_COARSE_LOCATION",
"android.permission.ACCESS_WIFI_STATE"},
1
);
}
}
final BluetoothManager bluetoothManager =
(BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
BluetoothAdapter bluetoothAdapter = bluetoothManager.getAdapter();
public BleConnect(BluetoothAdapter mBluetoothAdapter, Context mContext) {
this.mBluetoothAdapter = mBluetoothAdapter;
this.mContext = mContext;
}
/** * 搜索到新设备广播广播接收器 */ private final BroadcastReceiver searchReceiver = new BroadcastReceiver() { public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (BluetoothDevice.ACTION_FOUND.equals(action)) { // 这就是蓝牙设备 BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); //做出一些判断你要连接哪台蓝牙设备 TODO(); //获取到BlueToothGatt对象 mBluetoothGatt = device.connectGatt(mContext, false, mGattCallback); //搜索到设备直接连接(后面有函数代码) connectGatt(device); Log.d("juerany", "get: "+device.getName()+"--"+device.getAddress()); //停止搜索 mBluetoothAdapter.cancelDiscovery(); } } } };
/** * 连接成功或者失败的回调函数 */ public final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() { @Override public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) { Log.d(TAG, "onConnectionStateChange: "+newState); if (newState == BluetoothProfile.STATE_CONNECTED) { //bluetooth is connected so discover services Log.d(TAG, "onConnectionStateChange: "+mBluetoothGatt.getDevice().getName()); mBluetoothGatt.discoverServices(); } else if (newState == BluetoothProfile.STATE_DISCONNECTED) { //Bluetooth is disconnected Log.d(TAG, "onConnectionStateChange: disconnected"); } } @Override public void onServicesDiscovered(BluetoothGatt gatt, int status) { if (status == BluetoothGatt.GATT_SUCCESS) { // services are discoverd Log.d(TAG, "onServicesDiscovered: GATT_SUCCESS"); mBluetoothGatt = gatt; } List<BluetoothGattService> list = gatt.getServices(); for(BluetoothGattService service: list){ for(BluetoothGattCharacteristic characteristic : service.getCharacteristics()){ //获取到相应的服务UUID和特征UUID TODO(); } } } } @Override public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { if (status == BluetoothGatt.GATT_SUCCESS) { } } @Override public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) { } @Override public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { super.onCharacteristicWrite(gatt, characteristic, status); } };
/** * 向设备发送数据 * @param value * @return */ public boolean writeRXCharacteristic(byte[] value) { BluetoothGattService RxService = mBluetoothGatt.getService("服务UUID"); if (RxService == null) { //Service not supported Log.d(TAG, "writeRXCharacteristic: Service not supported"); return false; } BluetoothGattCharacteristic RxChar = RxService.getCharacteristic("特征UUID"); if (RxChar == null) { // service not supported Log.d(TAG, "writeRXCharacteristic: Service not supported"); return false; } RxChar.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT); RxChar.setValue(value); Log.d(TAG, "writeRXCharacteristic: "); return mBluetoothGatt.writeCharacteristic(RxChar); }
/**
* 搜索设备
*/
public void searchDevice(){
if (mBluetoothAdapter.isDiscovering()) {
mBluetoothAdapter.cancelDiscovery();
}
if(mBluetoothAdapter != null){
//注册搜索广播
IntentFilter foundFilter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
mContext.registerReceiver(searchReceiver, foundFilter);
//开始搜索
mBluetoothAdapter.startDiscovery();
}
}
先写这么多,之后有时间再更新 把队列加入进蓝牙发送里面,防止粘包。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。