藍芽通訊-搜尋附近的藍芽裝置
阿新 • • 發佈:2019-01-25
與其他裝置通訊通訊之前需要搜尋周圍的藍芽裝置。
怎麼搜尋呢??
1.如果資料中已經和某些藍芽裝置繫結,可以使用BluetoothAdapter.getBondedDevices();方法獲得已經繫結的藍芽裝置列表
2.搜尋周圍的藍芽裝置受用BluetoothAdapter.startDiscovery()方法
3.搜尋到的藍芽裝置都是通過廣播返回,so..。需要註冊廣播接收器來獲得已經搜尋到的藍芽裝置。
下面我們看一下demo:
我們在佈局檔案中放一個按鈕和一個顯示文字TextView。
我們點選Button時,開始搜尋附近的藍芽裝置。將搜尋到的藍芽裝置追加到TextView上,我們將繫結的和搜尋到的都顯示在TextView上。
佈局檔案:
JAVA檔案:<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <Button android:id="@+id/button_id" android:layout_width="fill_parent" android:layout_height="wrap_content" android:onClick="onClick_Search" android:text="搜尋藍芽裝置" /> <TextView android:id="@+id/tvDevices" android:layout_width="fill_parent" android:layout_below="@+id/button_id" android:layout_height="wrap_content" /> </RelativeLayout>
package com.example.search_bluetooth_devices; import java.util.Set; import android.os.Bundle; import android.app.Activity; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.view.Menu; import android.view.View; import android.view.Window; import android.widget.TextView; public class MainActivity extends Activity { private TextView mTextView; private BluetoothAdapter mBluetoothAdapter; private BroadcastReceiver mReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub String action = intent.getAction(); // 獲得已經搜尋到的藍芽裝置 if (action.equals(BluetoothDevice.ACTION_FOUND)) { BluetoothDevice device = intent .getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); // 搜尋到的不是已經繫結的藍芽裝置 if (device.getBondState() != BluetoothDevice.BOND_BONDED) { // 顯示在TextView上 mTextView.append(device.getName() + ":" + device.getAddress()+"\n"); } // 搜尋完成 } else if (action .equals(BluetoothAdapter.ACTION_DISCOVERY_FINISHED)) { setProgressBarIndeterminateVisibility(false); setTitle("搜尋藍芽裝置"); } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); setContentView(R.layout.activity_main); mTextView = (TextView) findViewById(R.id.tvDevices); mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); // 獲取所有已經繫結的藍芽裝置 Set<BluetoothDevice> devices = mBluetoothAdapter.getBondedDevices(); if (devices.size() > 0) { for (BluetoothDevice bluetoothDevice : devices) { mTextView.append(bluetoothDevice.getName() + ":" + bluetoothDevice.getAddress() + "\n\n"); } } // 註冊用以接收到已搜尋到的藍芽裝置的receiver IntentFilter mFilter = new IntentFilter(BluetoothDevice.ACTION_FOUND); registerReceiver(mReceiver, mFilter); // 註冊搜尋完時的receiver mFilter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); registerReceiver(mReceiver, mFilter); } @Override protected void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); //解除註冊 unregisterReceiver(mReceiver); } @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; } public void onClick_Search(View v) { setProgressBarIndeterminateVisibility(true); setTitle("正在掃描...."); // 如果正在搜尋,就先取消搜尋 if (mBluetoothAdapter.isDiscovering()) { mBluetoothAdapter.cancelDiscovery(); } // 開始搜尋藍芽裝置,搜尋到的藍芽裝置通過廣播返回 mBluetoothAdapter.startDiscovery(); } }
配置檔案:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.search_bluetooth_devices"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.search_bluetooth_devices.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
執行結果:
本機繫結的藍芽裝置一開始就顯示(第一張圖),點選搜尋藍芽裝置後(第2張圖)