我正在列出所有配对的设备,并且工作正常,但是现在想获取配对设备的蓝牙信号强度...我知道它将通过使用rssi获得,但无法在我的应用程序中连续实现。
请给我合适的代码作为我的代码...我的代码在这里...

public class Security extends Fragment implements OnClickListener{
     private BluetoothAdapter BA;
     private Set<BluetoothDevice>pairedDevices;

     ArrayList<String> mylist = new ArrayList<String>();

     //private Object ImageView;

     @Override
     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
     View v = inflater.inflate(R.layout.security, null);

     BA = BluetoothAdapter.getDefaultAdapter();

     /* starting the bluetooth*/
     on(v);
     pairedDevices = BA.getBondedDevices();
     //length=4;
     // int j=1;

     for(BluetoothDevice bt : pairedDevices) {
        mylist.add(bt.getName());
        length=j;
        j++;

        bt.getBondState();
    }

     return v;
     }

     @Override
     public void onResume() {
      super.onResume();
    // Toast.makeText(getActivity(), "On resume", Toast.LENGTH_LONG).show();
     }

     /*************************Bluetooth function****************************/

       public void on(View view){
          if (!BA.isEnabled()) {
             Intent turnOn = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
             startActivityForResult(turnOn, 0);
             Toast.makeText(getActivity(),"Turned on"
             ,Toast.LENGTH_LONG).show();
          } else{
            // Toast.makeText(getActivity(),"Already on",
           //  Toast.LENGTH_LONG).show();
             }
       }

       public void Discovery(View view) {
          if(BA.isDiscovering()) {
            BA.cancelDiscovery();
           }
       }

       public void list(View view){
              Toast.makeText(getActivity(),"Showing Paired Devices",
              Toast.LENGTH_SHORT).show();
           }

    @Override
    public void onClick(View v) {
        for(int j=0;j<length;j++) {
        if(v.getId()==j)
        Toast.makeText(getActivity(), mylist.get(j), Toast.LENGTH_LONG).show();
        //hand.update(run,1000);
        }

    }

}

最佳答案

您可以从以下代码中获取信号。

编码你的活动

@Override
public void onCreate(Bundle savedInstanceState) {
    .....
    // Registering Broadcast. this will fire when Bluetoothdevice Found
    registerReceiver(receiver, new IntentFilter(BluetoothDevice.ACTION_ACL_CONNECTED));
}

private final BroadcastReceiver BroadcastReceiver = new BroadcastReceiver(){
    @Override
    public void onReceive(Context context, Intent intent) {
        String mIntentAction = intent.getAction();
        if(BluetoothDevice.ACTION_ACL_CONNECTED.equals(mIntentAction)) {
            int RSSI = intent.getShortExtra(BluetoothDevice.EXTRA_RSSI,Short.MIN_VALUE);
            String mDeviceName = intent.getStringExtra(BluetoothDevice.EXTRA_NAME);
        }
    }
};


当您的设备连接到远程设备时,将执行此广播。
您还可以执行其他几种操作来触发此广播。看看here(BluetoothDevice)

检查以下链接以不断阅读RSSI

Tutorial to continuously measure the Bluetooth RSSI of a connected Android device (Java)

链接的输出:

关于android - Android不断获取配对设备的蓝牙信号强度,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22219036/

10-12 00:10
查看更多