- 藍(lán)牙通信-如果允許本地藍(lán)牙被附近的其它藍(lán)牙設(shè)備發(fā)現(xiàn)
如果本地的藍(lán)牙設(shè)備可以被附近的其它藍(lán)牙設(shè)備發(fā)現(xiàn),可以使用下面的方法,代碼中有注釋。
當(dāng)然需要你的藍(lán)牙設(shè)備設(shè)置一下,可以被附近的藍(lán)牙設(shè)備檢測(cè)到(一般為2分鐘)
在設(shè)置-藍(lán)牙中-選中可檢測(cè)性復(fù)選框。我的數(shù)據(jù)時(shí)android4.1.1,手機(jī)廠家不同或版本不同,可能有所不同。
eg:
JAVA:代碼:
[html]
package com.example.enabling_discoverability_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)牙被附近的其它藍(lán)牙設(shè)備發(fā)現(xiàn),如果沒(méi)有打開(kāi),就先打開(kāi)藍(lán)牙設(shè)備
// 設(shè)置讓自己的手機(jī)被其他藍(lán)牙設(shè)備發(fā)現(xiàn)
// 如果需要用戶確認(rèn)操作,不需要獲取底層藍(lán)牙服務(wù)實(shí)例,可以通過(guò)一個(gè)Intent來(lái)傳遞ACTION_REQUEST_DISCOVERABLE參數(shù),
// 這里通過(guò)startActivityForResult來(lái)強(qiáng)制獲取一個(gè)結(jié)果,重寫onActivityResult()方法獲取執(zhí)行結(jié)果,
// 返回結(jié)果有RESULT_OK和RESULT_CANCELLED分別代表開(kāi)啟和取消(失敗)
Intent mIntent = new Intent(
BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
startActivityForResult(mIntent, 1);
}
@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)牙被附近的其它藍(lán)牙設(shè)備發(fā)現(xiàn)", Toast.LENGTH_SHORT)
.show();
} else if (resultCode == RESULT_CANCELED) {
Toast.makeText(this, "不允許藍(lán)牙被附近的其它藍(lán)牙設(shè)備發(fā)現(xiàn)", 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.enabling_discoverability_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)牙被附近的其它藍(lán)牙設(shè)備發(fā)現(xiàn),如果沒(méi)有打開(kāi),就先打開(kāi)藍(lán)牙設(shè)備
// 設(shè)置讓自己的手機(jī)被其他藍(lán)牙設(shè)備發(fā)現(xiàn)
// 如果需要用戶確認(rèn)操作,不需要獲取底層藍(lán)牙服務(wù)實(shí)例,可以通過(guò)一個(gè)Intent來(lái)傳遞ACTION_REQUEST_DISCOVERABLE參數(shù),
// 這里通過(guò)startActivityForResult來(lái)強(qiáng)制獲取一個(gè)結(jié)果,重寫onActivityResult()方法獲取執(zhí)行結(jié)果,
// 返回結(jié)果有RESULT_OK和RESULT_CANCELLED分別代表開(kāi)啟和取消(失敗)
Intent mIntent = new Intent(
BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
startActivityForResult(mIntent, 1);
}
@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)牙被附近的其它藍(lán)牙設(shè)備發(fā)現(xiàn)", Toast.LENGTH_SHORT)
.show();
} else if (resultCode == RESULT_CANCELED) {
Toast.makeText(this, "不允許藍(lán)牙被附近的其它藍(lán)牙設(shè)備發(fā)現(xiàn)", 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;
}
}
配置文件:
[html]
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas./apk/res/android"
package="com.example.enabling_discoverability_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.enabling_discoverability_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>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas./apk/res/android"
package="com.example.enabling_discoverability_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.enabling_discoverability_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>
|