2014-12-05 164 views
3

有人可以检查这个代码的问题。我已经实施了一项服务并将一个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这样的方法,我们可以问“配对别针”的用户...我无法理解我失踪。

+0

你在哪里扫描设备? – 2014-12-05 12:43:14

+0

扫描我已完成我的活动,我正在通过BluetoothDevice对象进入服务连接到它。 – doga 2014-12-05 12:45:12

+0

你有无条件的吐司或登录onConnectionStateChange以确保它被调用吗? – 2014-12-05 13:46:46

回答

1

您的onBind方法返回null,因此您的主要活动无法与您的服务进行通信。

您的代码应按照下面,

@ PairedBleService

public class LocalBinder extends Binder 
{ 
    PairedBleService getService() 
    { 
     return PairedBleService.this; 
    }; 
}; 

@Override 
public IBinder onBind(Intent intent) 
{ 
    // TODO Auto-generated method stub 
    return mBinder; 
}; 

private final IBinder mBinder = new LocalBinder(); 

@主要活动

//put this inside onServiceConnected 

PairedBleService bleService = ((PairedBleService.LocalBinder) service).getService(); 

// Your code for connecting with BLE device 
.................................... 
.................................... 
+0

Thanx响应vikram ...但我无法理解呢..因为我能够连接到服务,但BluetoothGattCallback不呼叫,即Toast消息不显示onConnectionStateChange()........ – doga 2014-12-08 05:48:23

+0

请分享您的主要活动代码 – 2014-12-08 17:35:06

1

编辑确实帮助。你想从应用程序内配对...

BluetoothDevice有一个createBond() API层19(4.4)的方法,所以这留下了4.3这是第一个BLE Android版本,但没有很多剩下的那些。

这应该弹出用户的PIN对话框。一旦设备成功绑定,您就可以连接。要知道绑定何时完成,您需要有一个BroadcastReceiver,注册后才能收听ACTION_BOND_STATE_CHANGED。进来的Intent具有EXTRA_BOND_STATE,其可以是BOND_BONDED,BOND_BONDING_BOND_NONE

所以在你的onStartCommand你想getBondState(),如果它是BOND_NON你想createBond()。如果是BOND_BONDING,则需要等待与BroadcastReceiver的绑定,并在绑定后进行连接。如果是BOND_BONDED那么你可以连接。

相关问题