转自

蓝牙(BlueTooth)要求的最低版本是android2.0,由于Android模拟器不支持蓝牙,运行蓝牙的有关应用必须在真机上测试运行。

蓝牙是一种重要的短距离无线通信协议,广泛应用于各种设备(手机,医疗,汽车等)。蓝牙是比较常用的无线通信设备,早研究成为手机的标配。

在Android中,与蓝牙有关的类和接口在android.bluetooth包中。其中BluetoothAdapter是蓝牙中的核心类,

代表本地的蓝牙适配器设备。BluetoothAdapter类让用户能执行基本的蓝牙任务。例如: 初始化设备的搜索,查询可匹配的设备集,使用一个已知的MAC地址来初始化一个BluetoothDevice类,创建一个 BluetoothServerSocket类以监听其它设备对本机的连接请求等。

当我们使用蓝牙时会先判断当前手机是否打开了蓝牙,然后在进行相应的处理。

下面我们看看怎样打开蓝牙设备。

1.我们调用时除了需要考虑API Level至少为5外,还需注意添加相应的权限,比如使用通讯需要在androidmanifest.xml加入<uses-permissionandroid:name="android.permission.BLUETOOTH" />,而开关蓝牙需要android.permission.BLUETOOTH_ADMIN权限。

只要是有关蓝牙的应用程序这两个不可少。

2JAVA主要代码:

 

  1. package com.example.open_local_bluetooth;  
  2.   
  3. import android.os.Bundle;  
  4. import android.app.Activity;  
  5. import android.bluetooth.BluetoothAdapter;  
  6. import android.content.Intent;  
  7. import android.view.Menu;  
  8. import android.widget.Toast;  
  9.   
  10. public class MainActivity extends Activity {  
  11.   
  12.     @Override  
  13.     protected void onCreate(Bundle savedInstanceState) {  
  14.         super.onCreate(savedInstanceState);  
  15.         setContentView(R.layout.activity_main);  
  16.   
  17.         BluetoothAdapter mBluetoothAdapter = BluetoothAdapter  
  18.                 .getDefaultAdapter();  
  19.         if (mBluetoothAdapter == null) {  
  20.             Toast.makeText(this"本机没有找到蓝牙硬件或驱动!", Toast.LENGTH_SHORT).show();  
  21.             finish();  
  22.         }  
  23.         // 如果本地蓝牙没有开启,则开启  
  24.         if (!mBluetoothAdapter.isEnabled()) {  
  25.             // 我们通过startActivityForResult()方法发起的Intent将会在onActivityResult()回调方法中获取用户的选择,比如用户单击了Yes开启,  
  26.             // 那么将会收到RESULT_OK的结果,  
  27.             // 如果RESULT_CANCELED则代表用户不愿意开启蓝牙  
  28.             Intent mIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);  
  29.             startActivityForResult(mIntent, 1);  
  30.             // 用enable()方法来开启,无需询问用户(实惠无声息的开启蓝牙设备),这时就需要用到android.permission.BLUETOOTH_ADMIN权限。  
  31.             // mBluetoothAdapter.enable();  
  32.             // mBluetoothAdapter.disable();//关闭蓝牙  
  33.         }  
  34.     }  
  35.   
  36.     @Override  
  37.     protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
  38.         // TODO Auto-generated method stub  
  39.         super.onActivityResult(requestCode, resultCode, data);  
  40.         if (requestCode == 1) {  
  41.             if (resultCode == RESULT_OK) {  
  42.                 Toast.makeText(this"蓝牙已经开启", Toast.LENGTH_SHORT).show();  
  43.             } else if (resultCode == RESULT_CANCELED) {  
  44.                 Toast.makeText(this"不允许蓝牙开启", Toast.LENGTH_SHORT).show();  
  45.                 finish();  
  46.             }  
  47.         }  
  48.   
  49.     }  
  50.   
  51.     @Override  
  52.     public boolean onCreateOptionsMenu(Menu menu) {  
  53.         // Inflate the menu; this adds items to the action bar if it is present.  
  54.         getMenuInflater().inflate(R.menu.main, menu);  
  55.         return true;  
  56.     }  
  57.   
  58. }  


 


3配置文件:AndrodManifest.xml

 

 

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="com.example.open_local_bluetooth"  
  4.     android:versionCode="1"  
  5.     android:versionName="1.0" >  
  6.   
  7.     <uses-sdk  
  8.         android:minSdkVersion="8"  
  9.         android:targetSdkVersion="17" />  
  10.     <uses-permission android:name="android.permission.BLUETOOTH"/>  
  11.     <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>  
  12.   
  13.     <application  
  14.         android:allowBackup="true"  
  15.         android:icon="@drawable/ic_launcher"  
  16.         android:label="@string/app_name"  
  17.         android:theme="@style/AppTheme" >  
  18.         <activity  
  19.             android:name="com.example.open_local_bluetooth.MainActivity"  
  20.             android:label="@string/app_name" >  
  21.             <intent-filter>  
  22.                 <action android:name="android.intent.action.MAIN" />  
  23.   
  24.                 <category android:name="android.intent.category.LAUNCHER" />  
  25.             </intent-filter>  
  26.         </activity>  
  27.     </application>  
  28.   
  29. </manifest>  


 


运行结果:

 

arrow
arrow
    全站熱搜

    戮克 發表在 痞客邦 留言(0) 人氣()