Android不断获得成对设备的蓝牙信号强度

时间:2014-03-06 08:32:54

标签: android bluetooth

我正在列出所有配对的设备,它很好用,但现在想要获得成对设备的蓝牙信号强度...我知道它将通过使用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);
        }

    }

}

2 个答案:

答案 0 :(得分:2)

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

为您的活动编码

@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)

链接输出:

enter image description here

答案 1 :(得分:1)

然后,如果您想连续执行此操作,则需要在服务或线程中运行它。这样你甚至可以添加时隙或睡眠(等待)来测量RSSI。