Android学习蓝牙扫描
Posted dozeoo
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android学习蓝牙扫描相关的知识,希望对你有一定的参考价值。
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TableRow android:layout_height="wrap_content" android:layout_width="match_parent"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="刷新列表" android:id="@+id/flushBT"></Button> <TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/sum" android:textSize="20dp" android:layout_marginLeft="20dp" android:text="总计:"></TextView> </TableRow> <ListView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/listshow"></ListView> </LinearLayout> </androidx.constraintlayout.widget.ConstraintLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.bluetooth"> <uses-permission android:name="android.permission.BLUETOOTH"></uses-permission> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"></uses-permission> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
MainActivity.java
package com.example.bluetooth; import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import android.Manifest; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothManager; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.content.pm.PackageManager; import android.os.Build; import android.os.Bundle; import android.view.View; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { private ListView listView; private Button btn; private TextView textView; private BlueToothReceiver blueToothReceiver; private List<String> listdata; private ArrayAdapter adapter; private BluetoothManager bluetoothManager; private BluetoothAdapter bluetoothAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listView = findViewById(R.id.listshow); btn = findViewById(R.id.flushBT); textView = findViewById(R.id.sum); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){ if (this.checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},0x123); }else{ InitBT(); } } listdata = new ArrayList<>(); adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,listdata); listView.setAdapter(adapter); IntentFilter intentFilter = new IntentFilter(); intentFilter.addAction(BluetoothDevice.ACTION_FOUND); intentFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED); intentFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); blueToothReceiver = new BlueToothReceiver(); registerReceiver(blueToothReceiver,intentFilter); ScanBT(); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { ScanBT(); } }); } private void ScanBT(){ listdata.clear(); if (bluetoothAdapter.isDiscovering()){ bluetoothAdapter.cancelDiscovery(); } bluetoothAdapter.startDiscovery(); } private void InitBT(){ bluetoothManager = (BluetoothManager) getSystemService(BLUETOOTH_SERVICE); bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); if (bluetoothAdapter == null) { Toast.makeText(MainActivity.this,"蓝牙不可用",Toast.LENGTH_LONG).show(); return ; } if (!bluetoothAdapter.isEnabled()) { Toast.makeText(MainActivity.this,"蓝牙未开启",Toast.LENGTH_LONG).show(); return ; } } @Override protected void onDestroy() { unregisterReceiver(blueToothReceiver); super.onDestroy(); } public class BlueToothReceiver extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { if (intent.getAction().equals(BluetoothDevice.ACTION_FOUND)){ BluetoothDevice device = (BluetoothDevice)intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); short rssi = intent.getExtras().getShort(BluetoothDevice.EXTRA_RSSI); for (int i =0;i<listdata.size();i++) { if (listdata.get(i).contains(device.getAddress())) return; } listdata.add(device.getName()+"\\n"+device.getAddress()+"\\n"+String.valueOf(rssi)); adapter.notifyDataSetChanged(); } if (intent.getAction().equals(BluetoothAdapter.ACTION_DISCOVERY_FINISHED)){ textView.setText("总计:"+String.valueOf(listdata.size())); } } } @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { if (requestCode == 0x123){ InitBT(); } super.onRequestPermissionsResult(requestCode, permissions, grantResults); } }
对于我这个初学者来说,整个代码都比较简单,就不解释了
蓝牙强度rssi:
单位是dbm,蓝牙信号的强度RSSI = 10*log P,P代表接收到的信号功率。蓝牙会发送广播,距离大小会影响信号功率强弱。假设发射功率取最大值为1mw,那么RSSI的值为0,也就是说你的距离离蓝牙最近时在理想状态下所获取的RSSI的值为0,但在实际中基本不会存在这个理想状态,因此RSSI的值基本都为负数。
rssi可以用来定位蓝牙设备位置
运行效果如下,小米3 Android6.0;华为Mate10pro Android9.0,Android Studio3.5.0
以上是关于Android学习蓝牙扫描的主要内容,如果未能解决你的问题,请参考以下文章