手机蓝牙检测蓝牙设备信号强度(RSSI)

蓝牙( Bluetooth® ):是一种无线技术标准,可实现固定设备、移动设备和楼宇个人域网之间的短距离数据交换(使用2.4—2.485GHz的ISM波段的UHF无线电波)。蓝牙技术最初由电信巨头爱立信公司于1994年创制,当时是作为RS232数据线的替代方案。蓝牙可连接多个设备,克服了数据同步的难题。

RSSI:Received Signal Strength Indication接收的信号强度指示,无线发送层的可选部分,用来判定链接质量,以及是否增大广播发送强度。通过接收到的信号强弱测定信号点与接收点的距离,进而根据相应数据进行定位计算的一种定位技术。

本例是通过手机蓝牙对周围蓝牙设备进行搜索,显示出域用户输入的蓝牙设备名称相匹配的远程蓝牙设备的信号强度(RSSI)。

所需权限:

    <uses-permission android:name="android.permission.BLUETOOTH"/>
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
    <uses-permission android:name="android.permission.BLUETOOTH_PRIVILEGED"/>

 

布局文件activity_main.xml:

    <TextView
        android:id="@+id/showRssi"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="hello_man" />

    <Button
        android:id="@+id/open"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/showRssi"
        android:layout_below="@+id/showRssi"
        android:layout_marginTop="24dp"
        android:onClick="open"
        android:text="打开蓝牙" />

    <Button
        android:id="@+id/close"
        android:onClick="close"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/open"
        android:layout_alignBottom="@+id/open"
        android:layout_centerHorizontal="true"
        android:text="关闭蓝牙" />

    <EditText
        android:id="@+id/bluetoothName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/open"
        android:layout_marginTop="31dp"
        android:ems="10"
        android:hint="请输入蓝牙名称" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/show"
        android:onClick="show"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/close"
        android:layout_alignBottom="@+id/close"
        android:layout_alignParentRight="true"
        android:text="信号强度" />

 

布局图:

布局为三个按钮,一个textview,一个editview。

第一个按钮功能为打开蓝牙,第二个按钮为关闭蓝牙,第三个按钮为搜索蓝牙设备,textview用于显示蓝牙设备名称以及信号强度,editview用于输入用户希望检测信号强度的蓝牙设备名称。


MainActivity.java:

public class MainActivity extends Activity {
    BluetoothAdapter bAdapter;//声明蓝牙适配器
    EditText nameView;//声明edittext
    String blueName;//声明用户输入的蓝牙设备名称变量
    TextView showRssi;//声明textview用于显示信号强度信息
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bAdapter = BluetoothAdapter.getDefaultAdapter();//获取蓝牙适配器
        //设置过滤器,过滤因远程蓝牙设备被找到而发送的广播 BluetoothDevice.ACTION_FOUND
        IntentFilter iFilter=new IntentFilter();
        iFilter.addAction(BluetoothDevice.ACTION_FOUND);
        //设置广播接收器和安装过滤器
        registerReceiver(new foundReceiver(), iFilter);
        //获取控件对象
        nameView = (EditText) findViewById(R.id.bluetoothName);
        showRssi = (TextView) findViewById(R.id.showRssi);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    
    /**
     * 打开蓝牙
     * @param v
     */
    public void open(View v) {
        if(!bAdapter.isEnabled()){
            bAdapter.enable();
            Toast.makeText(getApplicationContext(), "蓝牙打开成功", 0).show();
        }else{
            Toast.makeText(getApplicationContext(), "蓝牙已经打开", 0).show();
        }
    }
    
    /**
     * 关闭蓝牙
     * @param v
     */
    public void close(View v) {
        if(bAdapter.isEnabled()){
            bAdapter.disable();
            Toast.makeText(getApplicationContext(), "蓝牙关闭成功", 0).show();
        }else{
            Toast.makeText(getApplicationContext(), "蓝牙已经关闭", 0).show();
        }
    }

    /**
     * 搜索远程蓝牙设备,获取editview的值
     * @param v
     */
    public void show(View v) {
        if(bAdapter.isEnabled()){
            blueName = nameView.getText().toString().trim();
            bAdapter.startDiscovery();
        }else{
            Toast.makeText(getApplicationContext(), "蓝牙未打开", 0).show();;
        }
    }
    
    /**
     * 内部类:当找到一个远程蓝牙设备时执行的广播接收者
     * @author Administrator
     *
     */
    class foundReceiver extends BroadcastReceiver{

        @Override
        public void onReceive(Context context, Intent intent) {
            // TODO Auto-generated method stub
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);//获取此时找到的远程设备对象
            if(blueName.equals(device.getName())){//判断远程设备是否与用户目标设备相同
                short rssi=intent.getExtras().getShort(BluetoothDevice.EXTRA_RSSI);//获取额外rssi值
                showRssi.setText(device.getName()+":"+rssi);//显示rssi到控件上
                bAdapter.cancelDiscovery();//关闭搜索
            }else{
                showRssi.setText("未发现设备“"+blueName+"”");
            }
        }
        
    }

 

Activity的代码总体分成两部分:①按钮功能的实现  ②获取设备rssi值并显示

1、获取本地蓝牙适配器

BluetoothAdapter  bAdapter = BluetoothAdapter.getDefaultAdapter();//获取蓝牙适配器

 

2、使用蓝牙适配器操作蓝牙

bAdapter.isEnabled()//判断蓝牙是否打开
bAdapter.enable();//打开蓝牙
bAdapter.disenable();//关闭蓝牙
bAdapter.startDiscovery();//搜索远程蓝牙设备(需要消耗大量资源,不建议频繁开关)
bAdapter.cancelDiscovery();//关闭搜索

 

 

 3、动态配置广播接收

        //设置过滤器,过滤因远程蓝牙设备被找到而发送的广播 BluetoothDevice.ACTION_FOUND
        IntentFilter iFilter=new IntentFilter();
        iFilter.addAction(BluetoothDevice.ACTION_FOUND);
        //设置广播接收器和安装过滤器
        registerReceiver(new foundReceiver(), iFilter);

 

BluetoothDevice.ACTION_FOUND 是配置广播时的一个字符串常量,意思是当有蓝牙设备被发现(未绑定)时发送广播,使用意图过滤器(IntentFilter)过滤。

 4、获取rssi

short rssi=intent.getExtras().getShort(BluetoothDevice.EXTRA_RSSI);//获取额外rssi值

 

5、广播中获取远程蓝牙设备对象(device)

BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);//获取此时找到的远程设备对象

 

posted @ 2017-10-02 17:01  sovagxa&静默  阅读(34073)  评论(0编辑  收藏  举报