小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

藍(lán)牙通信

 quasiceo 2015-09-19
 

藍(lán)牙(BlueTooth)要求的最低版本是android2.0,由于Android模擬器不支持藍(lán)牙,運(yùn)行藍(lán)牙的有關(guān)應(yīng)用必須在真機(jī)上測(cè)試運(yùn)行。

藍(lán)牙是一種重要的短距離無(wú)線通信協(xié)議,廣泛應(yīng)用于各種設(shè)備(手機(jī),醫(yī)療,汽車等)。藍(lán)牙是比較常用的無(wú)線通信設(shè)備,早研究成為手機(jī)的標(biāo)配。

在Android中,與藍(lán)牙有關(guān)的類和接口在android.bluetooth包中。其中BluetoothAdapter是藍(lán)牙中的核心類,

代表本地的藍(lán)牙適配器設(shè)備。BluetoothAdapter類讓用戶能執(zhí)行基本的藍(lán)牙任務(wù)。例如: 初始化設(shè)備的搜索,查詢可匹配的設(shè)備集,使用一個(gè)已知的MAC地址來(lái)初始化一個(gè)BluetoothDevice類,創(chuàng)建一個(gè) BluetoothServerSocket類以監(jiān)聽(tīng)其它設(shè)備對(duì)本機(jī)的連接請(qǐng)求等。

當(dāng)我們使用藍(lán)牙時(shí)會(huì)先判斷當(dāng)前手機(jī)是否打開(kāi)了藍(lán)牙,然后在進(jìn)行相應(yīng)的處理。

下面我們看看怎樣打開(kāi)藍(lán)牙設(shè)備。

1.我們調(diào)用時(shí)除了需要考慮API Level至少為5外,還需注意添加相應(yīng)的權(quán)限,比如使用通訊需要在androidmanifest.xml加入,而開(kāi)關(guān)藍(lán)牙需要android.permission.BLUETOOTH_ADMIN權(quán)限。

只要是有關(guān)藍(lán)牙的應(yīng)用程序這兩個(gè)不可少。

2JAVA主要代碼:


[java]
package com.example.open_local_bluetooth; 
 
import android.os.Bundle; 
import android.app.Activity; 
import android.bluetooth.BluetoothAdapter; 
import android.content.Intent; 
import android.view.Menu; 
import android.widget.Toast; 
 
public class MainActivity extends Activity { 
 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.activity_main); 
 
        BluetoothAdapter mBluetoothAdapter = BluetoothAdapter 
                .getDefaultAdapter(); 
        if (mBluetoothAdapter == null) { 
            Toast.makeText(this, "本機(jī)沒(méi)有找到藍(lán)牙硬件或驅(qū)動(dòng)!", Toast.LENGTH_SHORT).show(); 
            finish(); 
        } 
        // 如果本地藍(lán)牙沒(méi)有開(kāi)啟,則開(kāi)啟  
        if (!mBluetoothAdapter.isEnabled()) { 
            // 我們通過(guò)startActivityForResult()方法發(fā)起的Intent將會(huì)在onActivityResult()回調(diào)方法中獲取用戶的選擇,比如用戶單擊了Yes開(kāi)啟,  
            // 那么將會(huì)收到RESULT_OK的結(jié)果,  
            // 如果RESULT_CANCELED則代表用戶不愿意開(kāi)啟藍(lán)牙  
            Intent mIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); 
            startActivityForResult(mIntent, 1); 
            // 用enable()方法來(lái)開(kāi)啟,無(wú)需詢問(wèn)用戶(實(shí)惠無(wú)聲息的開(kāi)啟藍(lán)牙設(shè)備),這時(shí)就需要用到android.permission.BLUETOOTH_ADMIN權(quán)限。  
            // mBluetoothAdapter.enable();  
            // mBluetoothAdapter.disable();//關(guān)閉藍(lán)牙  
        } 
    } 
 
    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
        // TODO Auto-generated method stub  
        super.onActivityResult(requestCode, resultCode, data); 
        if (requestCode == 1) { 
            if (resultCode == RESULT_OK) { 
                Toast.makeText(this, "藍(lán)牙已經(jīng)開(kāi)啟", Toast.LENGTH_SHORT).show(); 
            } else if (resultCode == RESULT_CANCELED) { 
                Toast.makeText(this, "不允許藍(lán)牙開(kāi)啟", Toast.LENGTH_SHORT).show(); 
                finish(); 
            } 
        } 
 
    } 
 
    @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; 
    } 
 

package com.example.open_local_bluetooth;

import android.os.Bundle;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.view.Menu;
import android.widget.Toast;

public class MainActivity extends Activity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

  BluetoothAdapter mBluetoothAdapter = BluetoothAdapter
    .getDefaultAdapter();
  if (mBluetoothAdapter == null) {
   Toast.makeText(this, "本機(jī)沒(méi)有找到藍(lán)牙硬件或驅(qū)動(dòng)!", Toast.LENGTH_SHORT).show();
   finish();
  }
  // 如果本地藍(lán)牙沒(méi)有開(kāi)啟,則開(kāi)啟
  if (!mBluetoothAdapter.isEnabled()) {
   // 我們通過(guò)startActivityForResult()方法發(fā)起的Intent將會(huì)在onActivityResult()回調(diào)方法中獲取用戶的選擇,比如用戶單擊了Yes開(kāi)啟,
   // 那么將會(huì)收到RESULT_OK的結(jié)果,
   // 如果RESULT_CANCELED則代表用戶不愿意開(kāi)啟藍(lán)牙
   Intent mIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
   startActivityForResult(mIntent, 1);
   // 用enable()方法來(lái)開(kāi)啟,無(wú)需詢問(wèn)用戶(實(shí)惠無(wú)聲息的開(kāi)啟藍(lán)牙設(shè)備),這時(shí)就需要用到android.permission.BLUETOOTH_ADMIN權(quán)限。
   // mBluetoothAdapter.enable();
   // mBluetoothAdapter.disable();//關(guān)閉藍(lán)牙
  }
 }

 @Override
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  // TODO Auto-generated method stub
  super.onActivityResult(requestCode, resultCode, data);
  if (requestCode == 1) {
   if (resultCode == RESULT_OK) {
    Toast.makeText(this, "藍(lán)牙已經(jīng)開(kāi)啟", Toast.LENGTH_SHORT).show();
   } else if (resultCode == RESULT_CANCELED) {
    Toast.makeText(this, "不允許藍(lán)牙開(kāi)啟", Toast.LENGTH_SHORT).show();
    finish();
   }
  }

 }

 @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;
 }

}

 

 

3配置文件:AndrodManifest.xml


[html]
 
http://schemas./apk/res/android" 
    package="com.example.open_local_bluetooth" 
    android:versionCode="1" 
    android:versionName="1.0" > 
 
            android:minSdkVersion="8" 
        android:targetSdkVersion="17" /> 
     
     
 
            android:allowBackup="true" 
        android:icon="@drawable/ic_launcher" 
        android:label="@string/app_name" 
        android:theme="@style/AppTheme" > 
                    android:name="com.example.open_local_bluetooth.MainActivity" 
            android:label="@string/app_name" > 
             
                 
 
                 
           
 
         
     
 

 


http://schemas./apk/res/android"
    package="com.example.open_local_bluetooth"
    android:versionCode="1"
    android:versionName="1.0" >

            android:minSdkVersion="8"
        android:targetSdkVersion="17" />
   
   

            android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
                    android:name="com.example.open_local_bluetooth.MainActivity"
            android:label="@string/app_name" >
           
               

               
           


       
   

 

 


 

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多