搜尋附近的藍芽裝置,並將其名字和mac地址現在在textview中
阿新 • • 發佈:2019-02-04
這篇文章主要寫如何搜尋藍芽裝置
(2)搜尋藍芽裝置需要執行startDiscovery()這個方法,這個過程會大約持續12秒。
if (mBluetoothAdapter.isDiscovering()) {
mBluetoothAdapter.cancelDiscovery();
}
// 開始搜尋藍芽裝置,搜尋到的藍芽裝置通過廣播返回
mBluetoothAdapter.startDiscovery();
要執行這個方法必須註冊一個BroadcastReceiver,屬性BluetoothDevice.ACTION_FOUND的intent
註冊BroadcastReceiver用下面這句話完成,分別在呼叫開始和結束時使用private final BroadcastReceiver Receiver = new BroadcastReceiver(){ public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if(BluetoothDevice.ACTION_FOUND.equals(action)) { BluetoothDevice device =intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); if (device.getBondState() != BluetoothDevice.BOND_BONDED) { textview1.append(device.getName() + ":" + device.getAddress() + "\n\n"); } } } };
如果呼叫結束,還需要取消註冊IntentFilter Filter = new IntentFilter(BluetoothDevice.ACTION_FOUND); registerReceiver( Receiver , Filter); Filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); registerReceiver( Receiver , Filter);
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
//解除註冊
unregisterReceiver(Receiver);
}
下面是開啟藍芽裝置 顯示已經配對的藍芽裝置 ,搜尋附近的藍芽裝置,並將已配對的藍芽裝置顯示在textview中的佈局檔案和原始碼activity_main.xml
MainActivity<ScrollView 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"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" /> <TextView android:id="@+id/Textview1" android:layout_width="150dp" android:layout_height="wrap_content" android:layout_weight="1" /> </LinearLayout> </ScrollView>
package com.liuzuyi.bluetooth;
import java.util.Set;
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.os.Bundle;
import android.os.Parcelable;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private static final int REQUEST_ENABLE_BT = 1;
private Button button;
private TextView textview1;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button=(Button)findViewById(R.id.button1);
textview1=(TextView )findViewById(R.id.Textview1);
button.setOnClickListener( new blue());
IntentFilter Filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver( Receiver , Filter);
Filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
registerReceiver( Receiver , Filter);
}
private final BroadcastReceiver Receiver = new BroadcastReceiver(){
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(BluetoothDevice.ACTION_FOUND.equals(action))
{
BluetoothDevice device =intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
if (device.getBondState() != BluetoothDevice.BOND_BONDED) {
textview1.append(device.getName() + ":"
+ device.getAddress() + "\n\n");
}
}
}
};
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
//解除註冊
unregisterReceiver(Receiver);
}
class blue implements OnClickListener{
@Override
public void onClick(View v) {
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter == null)
{
Toast.makeText(MainActivity.this, "此裝置不支援藍芽傳輸功能!", Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(MainActivity.this, "此裝置支援藍芽傳輸功能!", Toast.LENGTH_SHORT).show();
if (!mBluetoothAdapter.isEnabled())
{
Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
enableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
startActivityForResult(enableIntent, REQUEST_ENABLE_BT);
Toast.makeText(MainActivity.this, "藍芽裝置已經開啟!", Toast.LENGTH_SHORT).show();
}
if (mBluetoothAdapter.isDiscovering()) {
mBluetoothAdapter.cancelDiscovery();
}
// 開始搜尋藍芽裝置,搜尋到的藍芽裝置通過廣播返回
mBluetoothAdapter.startDiscovery();
Set<BluetoothDevice> pairedDevices=mBluetoothAdapter.getBondedDevices();
if(pairedDevices.size() > 0){
for (BluetoothDevice bluetoothDevice : pairedDevices) {
textview1.append(bluetoothDevice.getName() + ":"
+ bluetoothDevice.getAddress() + "\n\n");
}
}
}
}
}
}
說明:點選按鈕,就會把未配對過的藍芽裝置的名字和mac地址顯示在textview中,如果想顯示所有的藍芽裝置就把onReceive方法中的if語句去掉。