Android開發之藍芽(Bluetooth)操作(二)--修改本機藍牙裝置的可見度,並掃描周圍可用的藍牙裝置

來源:互聯網
上載者:User

標籤:rri   keyword   機制   bsp   修改   can   xmlns   min   icon   

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

一. 修改本機藍牙裝置的可見度

二. 掃描周圍可用的藍牙裝置

 

 

Eg:

一.  資訊清單檔AdroidManifest.xml:

[java] view plain copy 
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.       package="com.se7en"  
  4.       android:versionCode="1"  
  5.       android:versionName="1.0">  
  6.     <uses-sdk android:minSdkVersion="8" />  
  7.   
  8.     <application android:icon="@drawable/icon" android:label="@string/app_name">  
  9.         <activity android:name=".MainActivity"  
  10.                   android:label="@string/app_name">  
  11.             <intent-filter>  
  12.                 <action android:name="android.intent.action.MAIN" />  
  13.                 <category android:name="android.intent.category.LAUNCHER" />  
  14.             </intent-filter>  
  15.         </activity>  
  16.     </application>  
  17.     <uses-permission android:name="android.permission.BLUETOOTH"/>  
  18.       
  19.     <!-若需要管理藍牙裝置,如修改可見度,則需以下的許可權->  
  20.     <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>  
  21. </manifest>  

二. 布局檔案: main.xml:

 

[java] view plain copy 
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7.     <TextView    
  8.         android:layout_width="fill_parent"   
  9.         android:layout_height="wrap_content"   
  10.         android:text="@string/hello"  
  11.         />  
  12.     <Button   
  13.         android:id="@+id/discoverButton"  
  14.         android:layout_width="fill_parent"  
  15.         android:layout_height="wrap_content"  
  16.         android:text="設定可見度"/>  
  17.     <Button   
  18.         android:id="@+id/scanButton"  
  19.         android:layout_width="fill_parent"  
  20.         android:layout_height="wrap_content"  
  21.         android:text="開始掃描"/>  
  22. </LinearLayout>  

三. MainActivity:

 

 

[java] view plain copy 
  1. import android.app.Activity;  
  2. import android.bluetooth.BluetoothAdapter;  
  3. import android.bluetooth.BluetoothDevice;  
  4. import android.content.BroadcastReceiver;  
  5. import android.content.Context;  
  6. import android.content.Intent;  
  7. import android.content.IntentFilter;  
  8. import android.os.Bundle;  
  9. import android.view.View;  
  10. import android.view.View.OnClickListener;  
  11. import android.widget.Button;  
  12.   
  13. public class MainActivity extends Activity {  
  14.     private Button discoverButton = null;  
  15.     private Button scanButton = null;  
  16.     private BluetoothAdapter adapter = null;  
  17.     private BluetoothReceiver bluetoothReceiver = null;  
  18.     /** Called when the activity is first created. */  
  19.     @Override  
  20.     public void onCreate(Bundle savedInstanceState) {  
  21.         super.onCreate(savedInstanceState);  
  22.         setContentView(R.layout.main);  
  23.           
  24.         adapter = BluetoothAdapter.getDefaultAdapter();  
  25.           
  26.         discoverButton = (Button)findViewById(R.id.discoverButton);  
  27.         scanButton = (Button)findViewById(R.id.scanButton);  
  28.         //修改藍牙裝置的可見度  
  29.         discoverButton.setOnClickListener(new OnClickListener(){  
  30.             @Override  
  31.             public void onClick(View view) {  
  32.             Intent discoverIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);  
  33.   
  34. //設定藍芽可見度,500表示可見時間(單位:秒),當值大於300時預設為300  
  35. discoverIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION,500);  
  36. startActivity(discoverIntent);  
  37.             }  
  38.         });  
  39.           
  40.         scanButton.setOnClickListener(new OnClickListener(){  
  41.             @Override  
  42.             public void onClick(View v) {  
  43.         //開始掃描周圍藍牙裝置,該方法是非同步呼叫並以廣播的機制返回,所以需要建立一個BroadcastReceiver來擷取資訊  
  44.                 adapter.startDiscovery();  
  45.             }  
  46.         });  
  47.           
  48.         //設定廣播接收的filter  
  49.         IntentFilter intentFilter = new IntentFilter(BluetoothDevice.ACTION_FOUND);  
  50.         //建立藍芽廣播資訊的receiver  
  51.         bluetoothReceiver = new BluetoothReceiver ();  
  52.         //註冊廣播接收器  
  53.         registerReceiver(bluetoothReceiver,intentFilter);  
  54.               
  55.     }  
  56.       
  57.     private class BluetoothReceiver extends BroadcastReceiver{  
  58.         @Override  
  59.         public void onReceive(Context context, Intent intent) {  
  60.             //獲得掃描到的遠程藍牙裝置  
  61.             BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);  
  62.             System.out.println(device.getAddress());  
  63.         }  
  64.           
  65.     }  
  66. }  

Android開發之藍芽(Bluetooth)操作(二)--修改本機藍牙裝置的可見度,並掃描周圍可用的藍牙裝置

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.