有人可以检查这个代码的问题。我已经实施了一项服务并将一个BluetoothDevice
对象传递给它(BluetoothDevice
对象通过intent时没有错误/问题)。然后,在onStartCommand()
,我打电话deviceToConnect.connectGatt(this,false,mGattCallback)
。但我的BluetoothGattCallback()
不工作(不打印任何东西)。代码简单直接。有人可以帮我调试它。BLE connectGatt()方法不报告BluetoothGattCallback ....中的任何内容?
编辑:我正在做Le设备扫描MainActivity()
和传递设备对象到服务连接到设备。
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蓝牙s/w,我在那里我能够连接。但是它要求配对引脚,并且一旦将其连接并显示在配对蓝牙设备中。有没有像connectGatt
这样的方法,我们可以问“配对别针”的用户...我无法理解我失踪。
你在哪里扫描设备? – 2014-12-05 12:43:14
扫描我已完成我的活动,我正在通过BluetoothDevice对象进入服务连接到它。 – doga 2014-12-05 12:45:12
你有无条件的吐司或登录onConnectionStateChange以确保它被调用吗? – 2014-12-05 13:46:46