转自:
蓝牙(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主要代码:
- 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, "本机没有找到蓝牙硬件或驱动!", Toast.LENGTH_SHORT).show();
- finish();
- }
- // 如果本地蓝牙没有开启,则开启
- if (!mBluetoothAdapter.isEnabled()) {
- // 我们通过startActivityForResult()方法发起的Intent将会在onActivityResult()回调方法中获取用户的选择,比如用户单击了Yes开启,
- // 那么将会收到RESULT_OK的结果,
- // 如果RESULT_CANCELED则代表用户不愿意开启蓝牙
- Intent mIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
- startActivityForResult(mIntent, 1);
- // 用enable()方法来开启,无需询问用户(实惠无声息的开启蓝牙设备),这时就需要用到android.permission.BLUETOOTH_ADMIN权限。
- // mBluetoothAdapter.enable();
- // mBluetoothAdapter.disable();//关闭蓝牙
- }
- }
- @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, "蓝牙已经开启", Toast.LENGTH_SHORT).show();
- } else if (resultCode == RESULT_CANCELED) {
- Toast.makeText(this, "不允许蓝牙开启", 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
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.example.open_local_bluetooth"
- android:versionCode="1"
- android:versionName="1.0" >
- <uses-sdk
- android:minSdkVersion="8"
- android:targetSdkVersion="17" />
- <uses-permission android:name="android.permission.BLUETOOTH"/>
- <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
- <application
- android:allowBackup="true"
- android:icon="@drawable/ic_launcher"
- android:label="@string/app_name"
- android:theme="@style/AppTheme" >
- <activity
- android:name="com.example.open_local_bluetooth.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>
- </manifest>
运行结果:
全站熱搜
留言列表