@Override public void onReceive(Context context, Intent intent) { final String action = intent.getAction(); if (BluetoothLeService.ACTION_GATT_CONNECTED.equals(action)) { mConnected = true; updateConnectionState(R.string.connected); invalidateOptionsMenu(); } else if (BluetoothLeService.ACTION_GATT_DISCONNECTED.equals(action)) { mConnected = false; updateConnectionState(R.string.disconnected); Intent scan_intent = new Intent(DisplaySoundActivity.this, DeviceScanActivity.class); startActivity(scan_intent); invalidateOptionsMenu(); // unregisterReceiver(mGattUpdateReceiver); finish(); } else if (BluetoothLeService.ACTION_GATT_SERVICES_DISCOVERED.equals(action)) { // Show all the supported services and characteristics on the user interface. // ** changed // services = mBluetoothLeService.getSupportedGattServices(); getCharacteristics(services); } else if (BluetoothLeService.ACTION_DATA_AVAILABLE.equals(action)) { onDataRecived(intent.getByteArrayExtra(BluetoothLeService.EXTRA_DATA)); } }
public void toggleConnect(View view) { if (!mConnected) { mBluetoothLeService.connect(mDeviceAddress); } else { mBluetoothLeService.disconnect(); } }
public void getBatteryLevel() { BluetoothGattService service = mBluetoothLeService.GetBluetoothGattService(BATTERY_SERVICE); if (service != null) { BluetoothGattCharacteristic batteryLevel = service.getCharacteristic(KEY_BATTERY_LEVEL); mBluetoothLeService.readCharacteristic(batteryLevel); } }
private void stopTreatment() { // send 0V command sendDataToDevice(EbandageGattAttributes.ZERO_VOLTS); voltageTextView.setText(""); polarityTextView.setText(""); max_volt = false; // default state polarityChanged = false; // default state POSITIVE timerStopped = true; // default timer stopped state // set button default state showMinutes(true); setButtonDisabled(true); increaseThread = null; decreaseThread = null; countDownTimer.cancel(); // send OFF command to device if (mBluetoothLeService.sendPolarityCharacteristics( hexStringToByteArray(EbandageGattAttributes.EBANDAGE_OFF))) { Log.i(TAG, "Device is turned off"); } else { Log.i(TAG, "Turning device off failed"); // Try again ! mBluetoothLeService.sendPolarityCharacteristics( hexStringToByteArray(EbandageGattAttributes.EBANDAGE_OFF)); } }
private void changePolarity() { // change polarity to negative boolean polarity = mBluetoothLeService.sendPolarityCharacteristics( hexStringToByteArray(EbandageGattAttributes.EBANDAGE_NEGATIVE)); if (polarity) { Log.i(TAG, "Polarity changed"); polarityChanged = true; Thread thread = new Thread( new Runnable() { @Override public void run() { try { // 1 seconds delay Thread.sleep(1); // send negative volt to device changeVolt(); } catch (InterruptedException e) { e.printStackTrace(); } } }); thread.start(); } else { Log.i(TAG, "Changing polarity failed"); mBluetoothLeService.sendPolarityCharacteristics( hexStringToByteArray(EbandageGattAttributes.EBANDAGE_NEGATIVE)); } }
public void listenToCaliperMeasurements(final BluetoothGattCharacteristic characteristic) { if (characteristic == null) { Log.e( TAG, "Haven't discovered this feature yet, please wait until it has connected once, and or try disconnecting and reconnecting."); return; } Log.d(TAG, "Starting to listen to notifications from " + characteristic.getUuid()); final int charaProp = characteristic.getProperties(); if ((charaProp | BluetoothGattCharacteristic.PROPERTY_READ) > 0) { // If there is an active notification on a characteristic, clear // it first so it doesn't update the data field on the user interface. if (mNotifyCharacteristic != null) { mBluetoothLeService.setCharacteristicNotification(mNotifyCharacteristic, false); mNotifyCharacteristic = null; } mBluetoothLeService.readCharacteristic(characteristic); } if ((charaProp | BluetoothGattCharacteristic.PROPERTY_NOTIFY) > 0) { mNotifyCharacteristic = characteristic; mBluetoothLeService.setCharacteristicNotification(characteristic, true); // Set descriptor to enable notifications BluetoothGattDescriptor descriptor = characteristic.getDescriptor( UUID.fromString(SCalEvoBluetoothSpecifications.CLIENT_CHARACTERISTIC_CONFIG)); if (descriptor == null) { Log.e( TAG, "descriptor " + SCalEvoBluetoothSpecifications.CLIENT_CHARACTERISTIC_CONFIG + "was null, looking for others."); List<BluetoothGattDescriptor> descriptors = characteristic.getDescriptors(); String uuid; for (BluetoothGattDescriptor aDescriptor : descriptors) { uuid = aDescriptor.getUuid().toString(); Log.e(TAG, "BluetoothGattDescriptor " + uuid); descriptor = aDescriptor; } if (descriptor == null) { Log.e( TAG, "Couldn't find any descriptors for this characteristic, cant enable notifications"); return; } else { Log.e(TAG, "Trying the last descriptor to enable notifications"); } } descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE); mBluetoothLeService.writeDescriptor(descriptor); Toast.makeText(this, "Ready", Toast.LENGTH_SHORT).show(); Log.e(TAG, "Ready to read from characteristic " + characteristic.getUuid()); } }
@Override public void onServiceConnected(ComponentName componentName, IBinder service) { mBluetoothLeService = ((BluetoothLeService.LocalBinder) service).getService(); if (!mBluetoothLeService.initialize()) { Log.e("onServiceConnected", "Unable to initialize Bluetooth"); ((Activity) context).finish(); } // Automatically connects to the device upon successful start-up initialization. mBluetoothLeService.connect(mDeviceAddress); }
@Override protected void onDestroy() { super.onDestroy(); // disconnect from bluetooth device mBluetoothLeService.disconnect(); // unbind the bleservice unbindService(mServiceConnection); // close gattServer mBluetoothLeService.close(); }
@Override public void onServiceConnected(ComponentName componentName, IBinder service) { mBluetoothLeService = ((BluetoothLeService.LocalBinder) service).getService(); if (!mBluetoothLeService.initialize()) { finish(); } // Automatically connects to the device upon successful start-up // initialization. // 根据蓝牙地址,连接设备 mBluetoothLeService.connect(mDeviceAddress); }
@Override public void onServiceConnected(ComponentName componentName, IBinder service) { Log.d(TAG, "onServiceConnected"); mBluetoothLeService = ((BluetoothLeService.LocalBinder) service).getService(); if (!mBluetoothLeService.initialize()) { Log.e(TAG, "Unable to initialize Bluetooth"); Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(enableBtIntent, BluetoothLeService.REQUEST_ENABLE_BT); Log.d(TAG, "requiring user to turn ON bluetooth"); return; } mBluetoothLeService.readAlarms(); }
private void sendDataToDevice(String hexStr) { Log.i(TAG, hexStr); byte[] hex = hexStringToByteArray(hexStr); // send command boolean status = mBluetoothLeService.send(hex); if (status) { Log.i(TAG, "Successful command"); } else { Log.i(TAG, "Sending command failed"); // try again ! mBluetoothLeService.send(hex); } }
private void getGattServices(List<BluetoothGattService> gattServices) { if (gattServices == null) return; String uuid = null; mModelNumberCharacteristic = null; mSerialPortCharacteristic = null; mCommandCharacteristic = null; mGattCharacteristics = new ArrayList<ArrayList<BluetoothGattCharacteristic>>(); // Loops through available GATT Services. for (BluetoothGattService gattService : gattServices) { uuid = gattService.getUuid().toString(); System.out.println("displayGattServices + uuid=" + uuid); List<BluetoothGattCharacteristic> gattCharacteristics = gattService.getCharacteristics(); ArrayList<BluetoothGattCharacteristic> charas = new ArrayList<BluetoothGattCharacteristic>(); // Loops through available Characteristics. for (BluetoothGattCharacteristic gattCharacteristic : gattCharacteristics) { charas.add(gattCharacteristic); uuid = gattCharacteristic.getUuid().toString(); if (uuid.equals(ModelNumberStringUUID)) { mModelNumberCharacteristic = gattCharacteristic; System.out.println( "mModelNumberCharacteristic " + mModelNumberCharacteristic.getUuid().toString()); } else if (uuid.equals(SerialPortUUID)) { mSerialPortCharacteristic = gattCharacteristic; System.out.println( "mSerialPortCharacteristic " + mSerialPortCharacteristic.getUuid().toString()); // updateConnectionState(R.string.comm_establish); } else if (uuid.equals(CommandUUID)) { mCommandCharacteristic = gattCharacteristic; System.out.println( "mSerialPortCharacteristic " + mSerialPortCharacteristic.getUuid().toString()); // updateConnectionState(R.string.comm_establish); } } mGattCharacteristics.add(charas); } if (mModelNumberCharacteristic == null || mSerialPortCharacteristic == null || mCommandCharacteristic == null) { Toast.makeText(mainContext, "Please select DFRobot devices", Toast.LENGTH_SHORT).show(); mConnectionState = connectionStateEnum.isToScan; onConectionStateChange(mConnectionState); } else { mSCharacteristic = mModelNumberCharacteristic; mBluetoothLeService.setCharacteristicNotification(mSCharacteristic, true); mBluetoothLeService.readCharacteristic(mSCharacteristic); } }
public void getCharacteristic(List<BluetoothGattService> gattServices) { this.gattServices = gattServices; String uuid = null; UUID uuuid = null; String uuidDescr = ""; BleNameResolver resolver = new BleNameResolver(); BluetoothGattCharacteristic characteristic = null; BluetoothGattService targetGattService = null; List<BluetoothGattCharacteristic> gattCharacteristics = null; // get target gattservice for (BluetoothGattService gattService : gattServices) { uuuid = gattService.getUuid(); uuid = gattService.getUuid().toString(); uuidDescr = resolver.resolveServiceName(uuid); gattCharacteristics = gattService.getCharacteristics(); if (uuid.equals(BATTERY_SERVICE.toString())) { targetGatBatteryService = gattService; BluetoothGattCharacteristic readGattCharacteristic = targetGatBatteryService.getCharacteristic(KEY_BATTERY_LEVEL); if (readGattCharacteristic != null) { mBluetoothLeService.setCharacteristicNotification(readGattCharacteristic, true); } } if (uuid.equals(targetServiceUuid.toString())) { targetGattService = gattService; BluetoothGattCharacteristic readGattCharacteristic = targetGattService.getCharacteristic(readUUID); if (readGattCharacteristic != null) { mBluetoothLeService.setCharacteristicNotification(readGattCharacteristic, true); } // break; } } if (targetGattService != null) { Toast.makeText(context, "get service ok", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(context, "not support this BLE module", Toast.LENGTH_SHORT).show(); // return ; } gattCharacteristics = targetGattService.getCharacteristics(); targetGattCharacteristic = targetGattService.getCharacteristic(targetCharacterUuid); BluetoothGattCharacteristic readGattCharacteristic = targetGattService.getCharacteristic(readUUID); if (readGattCharacteristic != null) mBluetoothLeService.setCharacteristicNotification(readGattCharacteristic, true); }
@Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.menu_connect: mBluetoothLeService.connect(mDeviceAddress); return true; case R.id.menu_disconnect: mBluetoothLeService.disconnect(); return true; case android.R.id.home: onBackPressed(); return true; } return super.onOptionsItemSelected(item); }
@Override public void onReceive(Context context, Intent intent) { final String action = intent.getAction(); if (BluetoothLeService.ACTION_GATT_CONNECTED.equals(action)) // Gatt连接成功 { mConnected = true; status = "connected"; // 更新连接状态 updateConnectionState(status); // log("ble device connected"); } else if (BluetoothLeService.ACTION_GATT_DISCONNECTED // Gatt连接失败 .equals(action)) { mConnected = false; status = "disconnected"; // 更新连接状态 updateConnectionState(status); // log("ble device disconnected"); } else if (BluetoothLeService.ACTION_GATT_SERVICES_DISCOVERED // 发现GATT服务器 .equals(action)) { // Show all the supported services and characteristics on the // user interface. // 获取设备的所有蓝牙服务 displayGattServices(mBluetoothLeService.getSupportedGattServices()); // System.out.println("BroadcastReceiver :"+ "device SERVICES_DISCOVERED"); } else if (BluetoothLeService.ACTION_DATA_AVAILABLE.equals(action)) // 有效数据 { // 处理发送过来的数据 // String revMsg = intent.getExtras().getString(BluetoothLeService.EXTRA_DATA); } }
void buttonScanOnClickProcess() { switch (mConnectionState) { case isNull: mConnectionState = connectionStateEnum.isScanning; onConectionStateChange(mConnectionState); scanLeDevice(true); mScanDeviceDialog.show(); break; case isToScan: mConnectionState = connectionStateEnum.isScanning; onConectionStateChange(mConnectionState); scanLeDevice(true); mScanDeviceDialog.show(); break; case isScanning: break; case isConnecting: break; case isConnected: mBluetoothLeService.disconnect(); mHandler.postDelayed(mDisonnectingOverTimeRunnable, 10000); // mBluetoothLeService.close(); mConnectionState = connectionStateEnum.isDisconnecting; onConectionStateChange(mConnectionState); break; case isDisconnecting: break; default: break; } }
private boolean sendData(byte[] data) { if (target_character != null) { target_character.setValue(data); return mBluetoothLeService.writeCharacteristic(target_character); } return false; }
@Override public void onReceive(Context context, Intent intent) { Log.d("RECEIVER", "Received!"); Toast t = Toast.makeText(context, "Sms reçu", Toast.LENGTH_SHORT); t.show(); Log.i(TAG, "Intent recieved: " + intent.getAction()); if (intent.getAction().equals(ACTION)) { Bundle bundle = intent.getExtras(); if (bundle != null) { Object[] pdus = (Object[]) bundle.get("pdus"); final SmsMessage[] messages = new SmsMessage[pdus.length]; for (int i = 0; i < pdus.length; i++) { messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]); } if (messages.length > -1) { Log.i(TAG, "Message recieved: " + messages[0].getMessageBody()); Intent intentForward = new Intent(context, ForwardService.class); // intentForward.putExtra("BLEService", service); Log.i(TAG, "Put extra : " + service.toString()); Log.i(TAG, "Put extra : " + messages[0].getMessageBody()); // intentForward.putExtra("Gatt", characteristic); intentForward.putExtra("Message", messages[0].getMessageBody()); context.startService(intentForward); } } } }
@Override public void run() { if (mConnectionState == connectionStateEnum.isDisconnecting) mConnectionState = connectionStateEnum.isToScan; onConectionStateChange(mConnectionState); mBluetoothLeService.close(); }
public void onResume() { if (!mConnected || mBluetoothLeService == null) return; ((Activity) context).registerReceiver(mGattUpdateReceiver, makeGattUpdateIntentFilter()); if (mBluetoothLeService != null) { final boolean result = mBluetoothLeService.connect(mDeviceAddress); Log.d("registerReceiver", "Connect request result=" + result); } }
@SuppressLint("DefaultLocale") @Override public void onReceive(Context context, Intent intent) { final String action = intent.getAction(); System.out.println("mGattUpdateReceiver->onReceive->action=" + action); if (BluetoothLeService.ACTION_GATT_CONNECTED.equals(action)) { mConnected = true; mHandler.removeCallbacks(mConnectingOverTimeRunnable); } else if (BluetoothLeService.ACTION_GATT_DISCONNECTED.equals(action)) { mConnected = false; mConnectionState = connectionStateEnum.isToScan; onConectionStateChange(mConnectionState); mHandler.removeCallbacks(mDisonnectingOverTimeRunnable); mBluetoothLeService.close(); } else if (BluetoothLeService.ACTION_GATT_SERVICES_DISCOVERED.equals(action)) { // Show all the supported services and characteristics on the user interface. for (BluetoothGattService gattService : mBluetoothLeService.getSupportedGattServices()) { System.out.println( "ACTION_GATT_SERVICES_DISCOVERED " + gattService.getUuid().toString()); } getGattServices(mBluetoothLeService.getSupportedGattServices()); } else if (BluetoothLeService.ACTION_DATA_AVAILABLE.equals(action)) { if (mSCharacteristic == mModelNumberCharacteristic) { if (intent .getStringExtra(BluetoothLeService.EXTRA_DATA) .toUpperCase() .startsWith("DF BLUNO")) { mBluetoothLeService.setCharacteristicNotification(mSCharacteristic, false); mSCharacteristic = mCommandCharacteristic; mSCharacteristic.setValue(mPassword); mBluetoothLeService.writeCharacteristic(mSCharacteristic); mSCharacteristic.setValue(mBaudrateBuffer); mBluetoothLeService.writeCharacteristic(mSCharacteristic); mSCharacteristic = mSerialPortCharacteristic; mBluetoothLeService.setCharacteristicNotification(mSCharacteristic, true); mConnectionState = connectionStateEnum.isConnected; onConectionStateChange(mConnectionState); } else { Toast.makeText(mainContext, "Please select DFRobot devices", Toast.LENGTH_SHORT) .show(); mConnectionState = connectionStateEnum.isToScan; onConectionStateChange(mConnectionState); } } else if (mSCharacteristic == mSerialPortCharacteristic) { onSerialReceived(intent.getStringExtra(BluetoothLeService.EXTRA_DATA)); } System.out.println( "displayData " + intent.getStringExtra(BluetoothLeService.EXTRA_DATA)); // // mPlainProtocol.mReceivedframe.append(intent.getStringExtra(BluetoothLeService.EXTRA_DATA)) ; // System.out.print("mPlainProtocol.mReceivedframe:"); // System.out.println(mPlainProtocol.mReceivedframe.toString()); } }
@Override public void onServiceConnected(ComponentName name, IBinder service) { mBluetoothLeService = ((BluetoothLeService.LocalBinder) service).getService(); if (!mBluetoothLeService.initialize()) { Log.e("Bluetooth", "Unable to initialize Bluetooth"); finish(); } // get bluetooth device device = getBleDevice(); if (device != null) { // Automatically connects to the device upon successful start-up initialization. boolean connection = mBluetoothLeService.connect(device.getAddress()); if (connection) { // set treatment view setVisibility(TREATMENT_SERVICE_NOT_FOUND); } } }
@Override public void onServiceConnected(ComponentName componentName, IBinder service) { System.out.println("mServiceConnection onServiceConnected"); mBluetoothLeService = ((BluetoothLeService.LocalBinder) service).getService(); if (!mBluetoothLeService.initialize()) { Log.e(TAG, "Unable to initialize Bluetooth"); ((Activity) mainContext).finish(); } }
@Override protected void onResume() { super.onResume(); registerReceiver(mGattUpdateReceiver, makeGattUpdateIntentFilter()); if (mBluetoothLeService != null) { final boolean result = mBluetoothLeService.connect(mDeviceAddress); Log.d(TAG, "Connect request result=" + result); } }
public void onStopProcess() { System.out.println("MiUnoActivity onStop"); if (mBluetoothLeService != null) { // mBluetoothLeService.disconnect(); // mHandler.postDelayed(mDisonnectingOverTimeRunnable, 10000); mHandler.removeCallbacks(mDisonnectingOverTimeRunnable); mBluetoothLeService.close(); } mSCharacteristic = null; }
public void sendMessage(View v) { final EditText sendEditText = (EditText) findViewById(R.id.send_value); String message = sendEditText.getText().toString(); if (mDataRequestOrCommandCharacteristic == null) { Log.e("BluetoothSend", "Cant send " + message); return; } Log.e("BluetoothSend", "Will send " + message); mBluetoothLeService.writeCharacteristic(mDataRequestOrCommandCharacteristic, message); listenToCaliperChanges(); }
@Override protected void onResume() { super.onResume(); // 绑定广播接收器 registerReceiver(mGattUpdateReceiver, makeGattUpdateIntentFilter()); if (mBluetoothLeService != null) { // 根据蓝牙地址,建立连接 final boolean result = mBluetoothLeService.connect(mDeviceAddress); } log("onresume"); }
// 发动到ble终端 public void sendToBle(String cmd) { try { target_chara.setValue(cmd); // 调用蓝牙服务的写特征值方法实现发送数据 mBluetoothLeService.writeCharacteristic(target_chara); // log("sending to ble : "+cmd); } catch (Exception e) { log("sendToBle 异常"); log(e.getMessage()); } }
@Override public boolean onChildClick( ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { if (mGattCharacteristics != null) { final BluetoothGattCharacteristic characteristic = mGattCharacteristics.get(groupPosition).get(childPosition); final int charaProp = characteristic.getProperties(); if ((charaProp | BluetoothGattCharacteristic.PROPERTY_READ) > 0) { // If there is an active notification on a characteristic, clear // it first so it doesn't update the data field on the user interface. if (mNotifyCharacteristic != null) { mBluetoothLeService.setCharacteristicNotification(mNotifyCharacteristic, false); mNotifyCharacteristic = null; } mBluetoothLeService.readCharacteristic(characteristic); } if ((charaProp | BluetoothGattCharacteristic.PROPERTY_NOTIFY) > 0) { listenToCaliperMeasurements(characteristic); } return true; } return false; }
public void onPauseProcess() { System.out.println("BLUNOActivity onPause"); scanLeDevice(false); mainContext.unregisterReceiver(mGattUpdateReceiver); mLeDeviceListAdapter.clear(); mConnectionState = connectionStateEnum.isToScan; onConectionStateChange(mConnectionState); mScanDeviceDialog.dismiss(); if (mBluetoothLeService != null) { mBluetoothLeService.disconnect(); mHandler.postDelayed(mDisonnectingOverTimeRunnable, 10000); // mBluetoothLeService.close(); } mSCharacteristic = null; }