请问有人能检查一下这段代码的问题吗?我已经实现了一个服务并传递了一个
编辑:我正在
BluetoothDevice
对象给它(通过 intent 传递 BluetoothDevice
对象时没有错误/问题)。然后,在 onStartCommand()
中,我调用了 deviceToConnect.connectGatt(this,false,mGattCallback)
。但是我的 BluetoothGattCallback()
没有起作用(没有打印任何内容)。代码简单明了。请问有人可以帮我调试一下吗?编辑:我正在
MainActivity()
中进行 Le 设备扫描,并将设备对象传递给一个连接设备的服务。public class PairedBleService extends Service
{
private BluetoothGatt mConnectedGatt;
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
Toast.makeText(this, "Service Started", Toast.LENGTH_SHORT).show();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
super.onStartCommand(intent, flags, startId);
BluetoothDevice deviceToConnect = (BluetoothDevice) intent.getParcelableExtra(DEVICE_TO_CONNECT);
mConnectedGatt = deviceToConnect.connectGatt(this, false, mGattCallback);
return START_STICKY;
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
Toast.makeText(this, "Service End", Toast.LENGTH_SHORT).show();
super.onDestroy();
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
private BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
if (status == BluetoothGatt.GATT_SUCCESS && newState == BluetoothProfile.STATE_CONNECTED) {
Toast.makeText(getApplicationContext(), "Peripheral connected", Toast.LENGTH_LONG).show();
} else if (status == BluetoothGatt.GATT_SUCCESS && newState == BluetoothProfile.STATE_DISCONNECTED) {
Toast.makeText(getApplicationContext(), "Peripheral Disconnected", Toast.LENGTH_LONG).show();
} else if (status != BluetoothGatt.GATT_SUCCESS) {
gatt.disconnect();
}
}
}
编辑:我试图使用标准的Android蓝牙软件连接我的信标(外设),并且我能够建立连接。但是它要求输入配对密码,一旦输入密码就会连接并显示在已配对的蓝牙设备中。是否有像connectGatt
这样的方法可以向用户询问“配对密码”... 我无法理解我错过了什么。