Android藍(lán)牙教程
藍(lán)牙 是一種無線交換數(shù)據(jù)的方式。愛掏網(wǎng) - it200.com Android提供了藍(lán)牙API來執(zhí)行多個(gè)任務(wù),例如:
- 掃描藍(lán)牙設(shè)備
- 連接和傳輸數(shù)據(jù)到其他設(shè)備
- 管理多個(gè)連接等
android.bluetooth包提供了許多接口類來處理藍(lán)牙,例如:
- BluetoothAdapter
- BluetoothDevice
- BluetoothSocket
- BluetoothServerSocket
- BluetoothClass
- BluetoothProfile
- BluetoothProfile.ServiceListener
- BluetoothHeadset
- BluetoothA2dp
- BluetoothHealth
- BluetoothHealthCallback
- BluetoothHealthAppConfiguration
BluetoothAdapter類
通過BluetoothAdapter類的幫助,我們可以執(zhí)行一些基本任務(wù),如啟動(dòng)設(shè)備發(fā)現(xiàn),查詢已配對(duì)的設(shè)備列表,創(chuàng)建BluetoothServerSocket實(shí)例以偵聽連接請(qǐng)求等。愛掏網(wǎng) - it200.com
BluetoothAdapter類的常量
BluetoothAdapter類提供了許多常量。愛掏網(wǎng) - it200.com其中一些如下所示:
- String ACTION_REQUEST_ENABLE
- String ACTION_REQUEST_DISCOVERABLE
- String ACTION_DISCOVERY_STARTED
- String ACTION_DISCOVERY_FINISHED
BluetoothAdapter類的方法
BluetoothAdapter類的常用方法如下:
- static synchronized BluetoothAdapter getDefaultAdapter() 返回BluetoothAdapter的實(shí)例。愛掏網(wǎng) - it200.com
- boolean enable() 如果藍(lán)牙適配器已禁用,則啟用藍(lán)牙適配器。愛掏網(wǎng) - it200.com
- boolean isEnabled() 如果藍(lán)牙適配器已啟用,則返回true。愛掏網(wǎng) - it200.com
- boolean disable() 如果藍(lán)牙適配器已啟用,則禁用藍(lán)牙適配器。愛掏網(wǎng) - it200.com
- String getName() 返回藍(lán)牙適配器的名稱。愛掏網(wǎng) - it200.com
- boolean setName(String name) 更改藍(lán)牙名稱。愛掏網(wǎng) - it200.com
- int getState() 返回本地藍(lán)牙適配器的當(dāng)前狀態(tài)。愛掏網(wǎng) - it200.com
Set <BluetoothDevice> getBondedDevices()
返回一組已配對(duì)的BluetoothDevice對(duì)象。愛掏網(wǎng) - it200.com- boolean startDiscovery() 啟動(dòng)發(fā)現(xiàn)過程。愛掏網(wǎng) - it200.com
Android藍(lán)牙示例:以編程方式啟用、禁用和使藍(lán)牙可被發(fā)現(xiàn)
你只需要寫幾行代碼,就可以啟用或禁用藍(lán)牙。愛掏網(wǎng) - it200.com
activity_main.xml
從面板中拖動(dòng)一個(gè)文本視圖和三個(gè)按鈕,現(xiàn)在activity_main.xml文件將如下所示:
<RelativeLayout xmlns:androclass="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<TextView android:text=""
android:id="@+id/out"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="30dp"
android:layout_marginTop="49dp"
android:text="TURN_ON" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button1"
android:layout_below="@+id/button1"
android:layout_marginTop="27dp"
android:text="DISCOVERABLE" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button2"
android:layout_below="@+id/button2"
android:layout_marginTop="28dp"
android:text="TURN_OFF" />
</RelativeLayout>
提供權(quán)限
您需要在AndroidManifest.xml文件中提供以下權(quán)限。愛掏網(wǎng) - it200.com
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
下面是AndroidManifest.xml文件的完整代碼。愛掏網(wǎng) - it200.com
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:androclass="http://schemas.android.com/apk/res/android"
package="com.example.bluetooth"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="16" />
<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.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>
Activity類
讓我們編寫代碼來啟用、禁用和使藍(lán)牙可發(fā)現(xiàn)。愛掏網(wǎng) - it200.com
package com.example.bluetooth;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private static final int REQUEST_ENABLE_BT = 0;
private static final int REQUEST_DISCOVERABLE_BT = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView out=(TextView)findViewById(R.id.out);
final Button button1 = (Button) findViewById(R.id.button1);
final Button button2 = (Button) findViewById(R.id.button2);
final Button button3 = (Button) findViewById(R.id.button3);
final BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter == null) {
out.append("device not supported");
}
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (!mBluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
if (!mBluetoothAdapter.isDiscovering()) {
//out.append("MAKING YOUR DEVICE DISCOVERABLE");
Toast.makeText(getApplicationContext(), "MAKING YOUR DEVICE DISCOVERABLE",
Toast.LENGTH_LONG);
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
startActivityForResult(enableBtIntent, REQUEST_DISCOVERABLE_BT);
}
}
});
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
mBluetoothAdapter.disable();
//out.append("TURN_OFF BLUETOOTH");
Toast.makeText(getApplicationContext(), "TURNING_OFF BLUETOOTH", Toast.LENGTH_LONG);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
聲明:所有內(nèi)容來自互聯(lián)網(wǎng)搜索結(jié)果,不保證100%準(zhǔn)確性,僅供參考。如若本站內(nèi)容侵犯了原著者的合法權(quán)益,可聯(lián)系我們進(jìn)行處理。