Android 如何获取某些指定设备(仅2或3个设备)的蓝牙RSSI,并将指定的RSSI用作参数?

Android 如何获取某些指定设备(仅2或3个设备)的蓝牙RSSI,并将指定的RSSI用作参数?,android,eclipse,parameters,bluetooth,rssi,Android,Eclipse,Parameters,Bluetooth,Rssi,这是我第一次在这里提问,我对编程还不熟悉,请原谅我的无知 我正在尝试制作一个应用程序,将我的android自动连接到指定的蓝牙设备(arduino) 该参数是指定蓝牙的RSSI级别(我使用的是2蓝牙),因此如果: rssi1=蓝牙A的RSSI级别 rssi2=蓝牙B的RSSI级别 我想要的是,如果rssi1>rssi2,那么我的android将连接到蓝牙A,反之亦然 我正在参考Memochipan上的代码,我知道我们可以在扫描时读取一些蓝牙设备的RSSI,但我不知道如何将RSSI作为我代码的参数

这是我第一次在这里提问,我对编程还不熟悉,请原谅我的无知

我正在尝试制作一个应用程序,将我的android自动连接到指定的蓝牙设备(arduino)

该参数是指定蓝牙的RSSI级别(我使用的是2蓝牙),因此如果:

rssi1=蓝牙A的RSSI级别

rssi2=蓝牙B的RSSI级别

我想要的是,如果rssi1>rssi2,那么我的android将连接到蓝牙A,反之亦然

我正在参考Memochipan上的代码,我知道我们可以在扫描时读取一些蓝牙设备的RSSI,但我不知道如何将RSSI作为我代码的参数

提前谢谢

这是我的密码

    public class Btdisc extends Activity {
        BluetoothSocket mmSocket;
        BluetoothDevice mmDevice;
        OutputStream mmOutputStream;
        InputStream mmInputStream;
        String ruang1 = "20:14:11:26:03:71";
        String ruang2 = "28:BA:B5:E1:B8:C0";
        int limit = -58;
        int rssi1a;
        int rssi2a;
        int stat;

        private BluetoothAdapter BTAdapter = BluetoothAdapter.getDefaultAdapter();
        private Set<BluetoothDevice> pairedDevices;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_btdisc);
            registerReceiver(receiver, new IntentFilter(BluetoothDevice.ACTION_FOUND));

            Button tombol = (Button) findViewById(R.id.button1);
            tombol.setOnClickListener(new OnClickListener(){
                public void onClick(View v) {
                    BTAdapter.startDiscovery();
                }
            });
        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            getMenuInflater().inflate(R.menu.btdisc, menu);
            return true;
        }

        private final BroadcastReceiver receiver = new BroadcastReceiver(){
            @Override
            public void onReceive(Context context, Intent intent) {
                String action = intent.getAction();
                if(BluetoothDevice.ACTION_FOUND.equals(action)) {

                     Set<BluetoothDevice> pairedDevices = BTAdapter.getBondedDevices();
                     if (pairedDevices.size() > 0) {
                        for (BluetoothDevice device : pairedDevices) {

                    rssi1a = intent.getShortExtra(BluetoothDevice.EXTRA_RSSI,Short.MIN_VALUE);
                    if (device.getAddress().equals(ruang1))
                    {     
                        TextView rssi1 = (TextView) findViewById(R.id.textView1);
                        rssi1.setText( "Ruang 1"  + " => " + rssi1a + "dBm\n\n");

                    }
                    rssi2a = intent.getShortExtra(BluetoothDevice.EXTRA_RSSI,Short.MIN_VALUE);
                    if (device.getAddress().equals(ruang2))
                    {
                        TextView rssi2 = (TextView) findViewById(R.id.textView2);
                        rssi2.setText("Ruang 2" + " => " + rssi2a + "dBm\n\n");
                    }

            // try {
            //  btconnect();
                //stat = 1;
            //} catch (IOException e) {
                //stat = 0;
            //e.printStackTrace();
            //}

            }
        }
    }
    }


    void btconnect() throws IOException

    {


            if (!BTAdapter.isEnabled()) {
                Intent enableBluetooth = new Intent(
                        BluetoothAdapter.ACTION_REQUEST_ENABLE);
                startActivityForResult(enableBluetooth, 1);

            }

            Set<BluetoothDevice> pairedDevices = BTAdapter.getBondedDevices();
            if (pairedDevices.size() > 0) {
                for (BluetoothDevice device : pairedDevices) {

                        if (device.getAddress().equals(ruang1) && rssi1a >= limit){
                        mmDevice = device;

                        break;
                    }
                }
            }

            UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb");

            mmSocket = mmDevice.createRfcommSocketToServiceRecord(uuid);

            mmSocket.connect();
            mmOutputStream = mmSocket.getOutputStream();
            mmInputStream = mmSocket.getInputStream();

        };
};
};
`
公共类Btdisc扩展活动{
蓝牙插座;
蓝牙设备;
输出流mmOutputStream;
输入流mmInputStream;
字符串ruang1=“20:14:11:26:03:71”;
字符串rung2=“28:BA:B5:E1:B8:C0”;
整数极限=-58;
int rssi1a;
int rssi2a;
int stat;
私有BluetoothAdapter BTAdapter=BluetoothAdapter.getDefaultAdapter();
专用配对设备;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_btdisc);
registerReceiver(接收器,新的IntentFilter(BluetoothDevice.ACTION_FOUND));
按钮tombol=(按钮)findViewById(R.id.button1);
setOnClickListener(新的OnClickListener(){
公共void onClick(视图v){
BTAdapter.startDiscovery();
}
});
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
getMenuInflater().充气(R.menu.btdisc,menu);
返回true;
}
专用最终BroadcastReceiver=新的BroadcastReceiver(){
@凌驾
公共void onReceive(上下文、意图){
String action=intent.getAction();
if(BluetoothDevice.ACTION_FOUND.equals(ACTION)){
Set pairedDevices=BTAdapter.getBondedDevices();
如果(pairedDevices.size()>0){
用于(蓝牙设备:pairedDevices){
rssi1a=intent.getShortExtra(蓝牙设备.EXTRA\RSSI,Short.MIN\u值);
if(device.getAddress()等于(ruang1))
{     
TextView rssi1=(TextView)findViewById(R.id.textView1);
rssi1.setText(“Ruang 1”+“=>”+rssi1a+“dBm\n\n”);
}
rssi2a=intent.getShortExtra(蓝牙设备.EXTRA\RSSI,Short.MIN\u值);
if(device.getAddress()等于(ruang2))
{
TextView rssi2=(TextView)findViewById(R.id.textView2);
rssi2.setText(“Ruang 2”+“=>”+rssi2a+“dBm\n\n”);
}
//试一试{
//btconnect();
//stat=1;
//}捕获(IOE异常){
//stat=0;
//e、 printStackTrace();
//}
}
}
}
}
void btconnect()引发IOException
{
如果(!BTAdapter.isEnabled()){
意图启用蓝牙=新意图(
BluetoothAdapter.ACTION\u REQUEST\u ENABLE);
startActivityForResult(启用蓝牙,1);
}
Set pairedDevices=BTAdapter.getBondedDevices();
如果(pairedDevices.size()>0){
用于(蓝牙设备:pairedDevices){
if(device.getAddress()等于(ruang1)和&rssi1a>=限制){
mmDevice=设备;
打破
}
}
}
UUID UUID=UUID.fromString(“000011101-0000-1000-8000-00805f9b34fb”);
mmSocket=mmDevice.createRfcommSocketToServiceRecord(uuid);
mmSocket.connect();
mmOutputStream=mmSocket.getOutputStream();
mmInputStream=mmSocket.getInputStream();
};
};
};
`