亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android開發(fā)中編寫藍(lán)牙相關(guān)功能的核心代碼講解

 更新時(shí)間:2016年02月27日 17:43:04   作者:時(shí)之沙  
這篇文章主要介紹了Android開發(fā)中編寫藍(lán)牙功能的核心部分講解,包括掃描和配對(duì)以及修改藍(lán)牙設(shè)備可見性等操作,需要的朋友可以參考下

一. 什么是藍(lán)牙(Bluetooth)?
1.1  BuleTooth是目前使用最廣泛的無(wú)線通信協(xié)議
1.2  主要針對(duì)短距離設(shè)備通訊(10m)
1.3  常用于連接耳機(jī),鼠標(biāo)和移動(dòng)通訊設(shè)備等.
二. 與藍(lán)牙相關(guān)的API
2.1 BluetoothAdapter:
代表了本地的藍(lán)牙適配器
2.2 BluetoothDevice
代表了一個(gè)遠(yuǎn)程的Bluetooth設(shè)備
三. 掃描已經(jīng)配對(duì)的藍(lán)牙設(shè)備(1)
注:必須部署在真實(shí)手機(jī)上,模擬器無(wú)法實(shí)現(xiàn)
首先需要在AndroidManifest.xml 聲明藍(lán)牙權(quán)限
<user-permission android:name="android.permission.BLUETOOTH" />
配對(duì)藍(lán)牙需要手動(dòng)操作:
1. 打開設(shè)置--> 無(wú)線網(wǎng)絡(luò) --> 藍(lán)牙 勾選開啟
2. 打開藍(lán)牙設(shè)置  掃描周圍已經(jīng)開啟的藍(lán)牙設(shè)備(可以與自己的筆記本電腦進(jìn)行配對(duì)),點(diǎn)擊進(jìn)行配對(duì)
 電腦上會(huì)彈出提示窗口: 添加設(shè)備
 顯示計(jì)算與設(shè)備之間的配對(duì)碼,要求確認(rèn)是否配對(duì)
 手機(jī)上也會(huì)顯示類似的提示.
四. 掃描已經(jīng)配對(duì)的藍(lán)牙設(shè)備(2)
4.1 獲得BluetoothAdapter對(duì)象
4.2 判斷當(dāng)前移動(dòng)設(shè)備中是否擁有藍(lán)牙
4.3 判斷當(dāng)前移動(dòng)設(shè)備中藍(lán)牙是否已經(jīng)打開
4.4 得到所有已經(jīng)配對(duì)的藍(lán)牙設(shè)備對(duì)象


藍(lán)牙配對(duì)實(shí)現(xiàn)的核心代碼如下:

MainActivity:

import java.util.Iterator; 
import java.util.Set; 
 
import android.app.Activity; 
import android.bluetooth.BluetoothAdapter; 
import android.bluetooth.BluetoothDevice; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
 
public class MainActivity extends Activity { 
  private Button button = null; 
  /** Called when the activity is first created. */ 
  @Override 
  public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
     
    button = (Button)findViewById(R.id.buttonId); 
    button.setOnClickListener(new OnClickListener(){ 
 
      @Override 
      public void onClick(View v) { 
        //獲得BluetoothAdapter對(duì)象,該API是android 2.0開始支持的 
        BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); 
        //adapter不等于null,說(shuō)明本機(jī)有藍(lán)牙設(shè)備 
        if(adapter != null){ 
          System.out.println("本機(jī)有藍(lán)牙設(shè)備!"); 
          //如果藍(lán)牙設(shè)備未開啟 
          if(!adapter.isEnabled()){ 
            Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); 
            //請(qǐng)求開啟藍(lán)牙設(shè)備 
            startActivity(intent); 
          } 
          //獲得已配對(duì)的遠(yuǎn)程藍(lán)牙設(shè)備的集合 
          Set<BluetoothDevice> devices = adapter.getBondedDevices(); 
          if(devices.size()>0){ 
            for(Iterator<BluetoothDevice> it = devices.iterator();it.hasNext();){ 
              BluetoothDevice device = (BluetoothDevice)it.next(); 
              //打印出遠(yuǎn)程藍(lán)牙設(shè)備的物理地址 
              System.out.println(device.getAddress()); 
            } 
          }else{ 
            System.out.println("還沒有已配對(duì)的遠(yuǎn)程藍(lán)牙設(shè)備!"); 
          } 
        }else{ 
          System.out.println("本機(jī)沒有藍(lán)牙設(shè)備!"); 
        } 
      } 
    }); 
  } 
} 


修改本機(jī)藍(lán)牙設(shè)備的可見性,并掃描周圍可用的藍(lán)牙設(shè)備
1. 修改本機(jī)藍(lán)牙設(shè)備的可見性
2. 掃描周圍可用的藍(lán)牙設(shè)備

Eg:
一.  清單文件AdroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
   package="com.se7en" 
   android:versionCode="1" 
   android:versionName="1.0"> 
  <uses-sdk android:minSdkVersion="8" /> 
 
  <application android:icon="@drawable/icon" android:label="@string/app_name"> 
    <activity android:name=".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> 
  <uses-permission android:name="android.permission.BLUETOOTH"/> 
   
  <!-若需要管理藍(lán)牙設(shè)備,如修改可見性,則需以下的權(quán)限-> 
  <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/> 
</manifest> 

二. 布局文件: main.xml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  android:orientation="vertical" 
  android:layout_width="fill_parent" 
  android:layout_height="fill_parent" 
  > 
  <TextView  
    android:layout_width="fill_parent"  
    android:layout_height="wrap_content"  
    android:text="@string/hello" 
    /> 
  <Button  
    android:id="@+id/discoverButton" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="設(shè)置可見性"/> 
  <Button  
    android:id="@+id/scanButton" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="開始掃描"/> 
</LinearLayout> 

三. MainActivity:

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.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
 
public class MainActivity extends Activity { 
  private Button discoverButton = null; 
  private Button scanButton = null; 
  private BluetoothAdapter adapter = null; 
  private BluetoothReceiver bluetoothReceiver = null; 
  /** Called when the activity is first created. */ 
  @Override 
  public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
     
    adapter = BluetoothAdapter.getDefaultAdapter(); 
     
    discoverButton = (Button)findViewById(R.id.discoverButton); 
    scanButton = (Button)findViewById(R.id.scanButton); 
    //修改藍(lán)牙設(shè)備的可見性 
    discoverButton.setOnClickListener(new OnClickListener(){ 
      @Override 
      public void onClick(View view) { 
      Intent discoverIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE); 
 
//設(shè)置藍(lán)牙可見性,500表示可見時(shí)間(單位:秒),當(dāng)值大于300時(shí)默認(rèn)為300 
discoverIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION,500); 
startActivity(discoverIntent); 
      } 
    }); 
     
    scanButton.setOnClickListener(new OnClickListener(){ 
      @Override 
      public void onClick(View v) { 
    //開始掃描周圍藍(lán)牙設(shè)備,該方法是異步調(diào)用并以廣播的機(jī)制返回,所以需要?jiǎng)?chuàng)建一個(gè)BroadcastReceiver來(lái)獲取信息 
        adapter.startDiscovery(); 
      } 
    }); 
     
    //設(shè)定廣播接收的filter 
    IntentFilter intentFilter = new IntentFilter(BluetoothDevice.ACTION_FOUND); 
    //創(chuàng)建藍(lán)牙廣播信息的receiver 
    bluetoothReceiver = new BluetoothReceiver (); 
    //注冊(cè)廣播接收器 
    registerReceiver(bluetoothReceiver,intentFilter); 
       
  } 
   
  private class BluetoothReceiver extends BroadcastReceiver{ 
    @Override 
    public void onReceive(Context context, Intent intent) { 
      //獲得掃描到的遠(yuǎn)程藍(lán)牙設(shè)備 
      BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); 
      System.out.println(device.getAddress()); 
    } 
     
  } 
} 

相關(guān)文章

最新評(píng)論