如何将BluetoothGatt数据发送到另一个活动。
在android studio中,我尝试连接BLE设备,尝试了Intent.putExtra,但无法解决。
btn2.setOnClickListener(new View.OnClickListener() {
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
public void onClick(View v) {
btn1.setBackground(getDrawable(R.drawable.buttonshape));
btn2.setBackground(getDrawable(R.drawable.underline));
btn3.setBackground(getDrawable(R.drawable.buttonshape));
Intent intent = new Intent(getApplicationContext(), LEDActivity.class);
intent.putExtra("connected", connected_);
Intent intent1 = new Intent(SettingActivity.this, LEDActivity.class);
**intent1.putExtra("blegatt", ble_gatt_.getDevice());
//intent1.putExtra("blegatt", String.valueOf(ble_gatt_));**
startActivity(intent);
overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
}
}
private void sendData( View v ) {
// check connection
Intent intent = getIntent();
boolean connected = intent.getBooleanExtra("connected", false);
**BluetoothGatt ble_gatt = intent.getParcelableExtra("blegatt");**
//BluetoothGatt ble_gatt = getIntent().getExtras().getParcelable("blegatt");
Toast.makeText(this,"ble_gatt_ -> " + ble_gatt, Toast.LENGTH_LONG).show();
if ( !connected ) {
Log.e( TAG, "Failed to sendData due to no connection" );
return;
}
// find command characteristics from the GATT server
BluetoothGattCharacteristic cmd_characteristic= BluetoothUtils.findCommandCharacteristic( ble_gatt );
// disconnect if the characteristic is not found
if( cmd_characteristic == null ) {
Log.e( TAG, "Unable to find cmd characteristic" );
set_.disconnectGattServer();
return;
}
// start stimulation
startStimulation( cmd_characteristic, 1 );
}
我希望完美地传递BluetoothGatt的数据,但实际我的代码传递null