我有两个问题。我想连接我的服务。我可以连接,但我不能断开服务时{i}我的应用程序将崩溃。其他问题是我不能发送值到RXchar .... ..
所有问题都是显而易见的mService.disconnect();
这是断开连接错误
RxChar错误
我的代码_ble_fragment
on a null object reference
DeviceList& UartService代码:
switch1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
try {
//send data to service
if (isChecked) {
message = "1";
} else {
message = "0";
}
value = message.getBytes("UTF-8");
sendText.setText(message);
mService.writeRXCharacteristic(value);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
scanButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.i("click", "按下去了");
if (!mBtAdapter.isEnabled()) {
Log.i(TAG, "onClick - BT not enabled yet");
Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableIntent, REQUEST_ENABLE_BT);
} else {
if (scanButton.getText().equals("Connect")) {
Log.i("scan", "open device");
switch1.setEnabled(true);
//Connect button pressed, open DeviceListActivity class, with popup windows that scan for devices
Intent newIntent = new Intent(getActivity(), DeviceListActivity.class);
startActivity(newIntent);
} else {
//Disconnect button pressed
mService.disconnect();
Log.i("Diconnect", "Disconnect Ble");
// mHandler.removeCallbacks(runnable);
}
}
}
});
我能做什么?
答案 0 :(得分:0)
你可以试试这个:
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if(bluetoothAdapter.isEnabled()){
bluetoothAdapter.disable();
}
你需要在下面添加两个权限:
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>