標籤:broadcastreceiver mac intentfilter startdi
上一篇文章已經寫了如何開啟藍牙裝置,顯示已經配對成功的藍牙裝置,http://blog.csdn.net/liuzuyi200/article/details/37740401
這篇文章主要寫如何搜尋藍牙裝置
(2)搜尋藍牙裝置需要執行startDiscovery()這個方法,這個過程會大約持續12秒。
if (mBluetoothAdapter.isDiscovering()) { mBluetoothAdapter.cancelDiscovery(); } // 開始搜尋藍牙裝置,搜尋到的藍牙裝置通過廣播返回 mBluetoothAdapter.startDiscovery(); 要執行這個方法必須註冊一個BroadcastReceiver,屬性BluetoothDevice.ACTION_FOUND的intent
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"); } }}};註冊BroadcastReceiver用下面這句話完成,分別在調用開始和結束時使用
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
<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>
MainActivity
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{@Overridepublic 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語句去掉。