@Override public void onCreate(Bundle savedInstanceState) { appState = (GlobalVar) getApplicationContext(); // 获得全局变量 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); // 设置成竖屏 super.onCreate(savedInstanceState); // getActionBar().setTitle(R.string.title_devices); requestWindowFeature(Window.FEATURE_NO_TITLE); // 去掉title // mHandler = new Handler(); // Use this check to determine whether BLE is supported on the device. Then you can // selectively disable BLE-related features. if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) { Toast.makeText(this, R.string.ble_not_supported, Toast.LENGTH_SHORT).show(); finish(); } // Initializes a Bluetooth adapter. For API level 18 and above, get a reference to // BluetoothAdapter through BluetoothManager. final BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE); mBluetoothAdapter = bluetoothManager.getAdapter(); appState.BluetoothAdapter = mBluetoothAdapter; // Checks if Bluetooth is supported on the device. if (mBluetoothAdapter == null) { Toast.makeText(this, R.string.error_bluetooth_not_supported, Toast.LENGTH_SHORT).show(); finish(); return; } // 开启蓝牙 mBluetoothAdapter.enable(); mBLE_reciv = new BluetoothLeClassDianzichen(this); appState.mBLE_reciv_dianzichen = mBLE_reciv; // appState.init_BluetoothLeClassDianzichen(); if (!mBLE_reciv.initialize()) { Log.e(TAG, "Unable to initialize Bluetooth"); finish(); } // 发现BLE终端的Service时回调 mBLE_reciv.setOnServiceDiscoverListener(mOnServiceDiscover); // 收到BLE终端数据交互的事件 mBLE_reciv.setOnDataAvailableListener(mOnDataAvailable); mBLE_send = new BluetoothLeClassDianzichen(this); appState.mBLE_send_dianzichen = mBLE_send; if (!appState.mBLE_send_dianzichen.initialize()) { Log.e(TAG, "Unable to initialize Bluetooth"); finish(); } // 发现BLE终端的Service时回调 mBLE_send.setOnServiceDiscoverListener(mOnServiceDiscover); // 收到BLE终端数据交互的事件 mBLE_send.setOnDataAvailableListener(mOnDataAvailable); }
@Override protected void onDestroy() { super.onDestroy(); scanLeDevice(false); mLeDeviceListAdapter.clear(); mBLE_send.disconnect(); mBLE_reciv.disconnect(); mBLE_send.close(); mBLE_reciv.close(); }
@Override protected void onListItemClick(ListView l, View v, int position, long id) { final BluetoothDevice device = mLeDeviceListAdapter.getDevice(position); if (device == null) return; if (mScanning) { mBluetoothAdapter.stopLeScan(mLeScanCallback); mScanning = false; } appState.deviceAddress = device.getAddress(); mBLE_reciv.connect(device.getAddress()); mBLE_send.connect(device.getAddress()); }
private void displayGattServices(List<BluetoothGattService> gattServices) { if (gattServices == null) return; for (BluetoothGattService gattService : gattServices) { // -----Service的字段信息-----// int type = gattService.getType(); Log.e(TAG, "-->service type:" + Utils.getServiceType(type)); Log.e(TAG, "-->includedServices size:" + gattService.getIncludedServices().size()); Log.e(TAG, "-->service uuid:" + gattService.getUuid()); // -----Characteristics的字段信息-----// List<BluetoothGattCharacteristic> gattCharacteristics = gattService.getCharacteristics(); for (final BluetoothGattCharacteristic gattCharacteristic : gattCharacteristics) { Log.e(TAG, "---->char uuid:" + gattCharacteristic.getUuid()); int permission = gattCharacteristic.getPermissions(); Log.e(TAG, "---->char permission:" + Utils.getCharPermission(permission)); int property = gattCharacteristic.getProperties(); Log.e(TAG, "---->char property:" + Utils.getCharPropertie(property)); byte[] data = gattCharacteristic.getValue(); if (data != null && data.length > 0) { Log.e(TAG, "---->char value:" + Utils.bytesToHexString(data)); } if (gattService.getUuid().toString().equals(UUID_KEY_SERVICE)) { // 如果是温度计的service // UUID_KEY_DATA是可以跟蓝牙模块串口通信的Characteristic if (gattCharacteristic.getUuid().toString().equals(UUID_KEY_DATA_RECIV)) { // 测试读取当前Characteristic数据,会触发mOnDataAvailable.onCharacteristicRead() mHandler.postDelayed( new Runnable() { @Override public void run() { mBLE_reciv.readCharacteristic(gattCharacteristic); } }, 500); // 接受Characteristic被写的通知,收到蓝牙模块的数据后会触发mOnDataAvailable.onCharacteristicWrite() mBLE_reciv.setCharacteristicNotification(gattCharacteristic, true); BluetoothGattDescriptor localBluetoothGattDescriptor = gattCharacteristic.getDescriptor(UUID.fromString(CCC)); localBluetoothGattDescriptor.setValue( BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE); mBluetoothGatt.writeDescriptor(localBluetoothGattDescriptor); appState.gattCharacteristic_reciv_dianzichen = gattCharacteristic; // 设置数据内容 // gattCharacteristic.setValue("0"); // 往蓝牙模块写入数据 // appState.mBLE_reciv_dianzichen.writeCharacteristic(gattCharacteristic); } // UUID_KEY_DATA是可以跟蓝牙模块串口通信的Characteristic if (gattCharacteristic.getUuid().toString().equals(UUID_KEY_DATA_SEND)) { // 测试读取当前Characteristic数据,会触发mOnDataAvailable.onCharacteristicRead() mHandler.postDelayed( new Runnable() { @Override public void run() { mBLE_send.readCharacteristic(gattCharacteristic); // Log.i("info", "appState.mBLE_send_dianzichen 尝试读数据"); } }, 500); // 接受Characteristic被写的通知,收到蓝牙模块的数据后会触发mOnDataAvailable.onCharacteristicWrite() mBLE_send.setCharacteristicNotification(gattCharacteristic, true); // 设置数据内容 // gattCharacteristic.setValue("send data->"); // 往蓝牙模块写入数据 // mBLE_send.writeCharacteristic(gattCharacteristic); if (!appState.firstActivityRunning) { appState.gattCharacteristic_send_dianzichen = gattCharacteristic; appState.file.write2SDFromInput("inurse/", "Dianzichen.txt", appState.deviceAddress); Intent it = new Intent(this, DianzichenActivity.class); startActivityForResult(it, 0); // 配合onActivityResult,当FirstActivity退出时获得一个0值,然后自己也退出 // moveTaskToBack(true); //这一句是整个程序返回桌面 appState.firstActivityRunning = true; } } } // 结束 if ( gattService.getUuid().toString().equals(UUID_KEY_SERVICE) ) // -----Descriptors的字段信息-----// List<BluetoothGattDescriptor> gattDescriptors = gattCharacteristic.getDescriptors(); for (BluetoothGattDescriptor gattDescriptor : gattDescriptors) { Log.e(TAG, "-------->desc uuid:" + gattDescriptor.getUuid()); int descPermission = gattDescriptor.getPermissions(); Log.e(TAG, "-------->desc permission:" + Utils.getDescPermission(descPermission)); byte[] desData = gattDescriptor.getValue(); if (desData != null && desData.length > 0) { Log.e(TAG, "-------->desc value:" + Utils.bytesToHexString(data)); } // soloman 接收的uuid要设置CCC通知enable,并回写到BluetoothGatt // if (gattDescriptor.getUuid().toString().equals(CCC)){ // gattDescriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE); // mBluetoothGatt.writeDescriptor(gattDescriptor); // } } } } // }
@Override public void onServiceDiscover(BluetoothGatt gatt) { mBluetoothGatt = gatt; // 搜索到服务后,给全局bluetoothgatt赋值 displayGattServices(mBLE_send.getSupportedGattServices()); displayGattServices(mBLE_reciv.getSupportedGattServices()); }