2011-08-24 77 views
3

我试图通过点击一个按钮来获取连接的蓝牙设备的当前RSSI值。但它总是只返回-32768!不知道什么是错的!但是,第一次连接时,我能够获得正确的RSSI。蓝牙的Android RSSI值总是返回-32768?

private Button.OnClickListener buttonRSSIOnClickListener = new Button.OnClickListener(){ 
    @Override 
    public void onClick(View arg0) { 
    // TODO Auto-generated method stub 
    Intent intent = new Intent(BluetoothDevice.ACTION_FOUND); 
    short rssi = intent.getShortExtra(BluetoothDevice.EXTRA_RSSI,Short.MIN_VALUE); 
    Toast.makeText(getApplicationContext()," RSSI: " + rssi + "dBm", Toast.LENGTH_SHORT).show(); 

    }}; 

任何人都可以帮助我吗?

回答

9

这不是你如何使用意图。由于RSSI不在您刚刚创建的Intent中,并且您指定的默认结果是Short.MIN_VALUE(-32768),您将获得-32768。

您需要创建子类BroadcastReceiver,并创建一个IntentFilter(或使用清单),以便您收到BluetoothDevice.ACTION_FOUND的意图。

你将无法做到这一点“点击一个按钮。”你只有在Android生成ACTION_FOUND时才会得到它。

这里是关闭的东西。没有自己运行它。

在的onCreate():

registerReceiver(receiver, new IntentFilter(BluetoothDevice.ACTION_FOUND)); 

在别处:

private final BroadcastReceiver receiver = new BroadcastReceiver(){ 
    @Override 
    public void onReceive(Context context, Intent intent) { 

     String action = intent.getAction(); 
     if(BluetoothDevice.ACTION_FOUND.equals(action)) { 
      short rssi = intent.getShortExtra(BluetoothDevice.EXTRA_RSSI,Short.MIN_VALUE); 
      Toast.makeText(getApplicationContext()," RSSI: " + rssi + "dBm", Toast.LENGTH_SHORT).show(); 
     } 
    } 
}; 

编辑:其实你也许可以做到这一点点播,如果你从内部的onClick您BluetoothAdapter调用startDiscovery() ()。这应该为它发现的每个设备触发ACTION_FOUND。

+0

接收器不工作...相反,当我过滤RSSI,然后lib类打印登录“onScanResult”...任何想法如何可以得到结果RSSI结果连接设备..? – CoDe