Android藍牙教程含代碼

Android藍牙教程

藍牙 是一種無線交換數據的方式。愛掏網 - it200.com Android提供了藍牙API來執行多個任務,例如:

  • 掃描藍牙設備
  • 連接和傳輸數據到其他設備
  • 管理多個連接等

android.bluetooth包提供了許多接口類來處理藍牙,例如:

  • BluetoothAdapter
  • BluetoothDevice
  • BluetoothSocket
  • BluetoothServerSocket
  • BluetoothClass
  • BluetoothProfile
  • BluetoothProfile.ServiceListener
  • BluetoothHeadset
  • BluetoothA2dp
  • BluetoothHealth
  • BluetoothHealthCallback
  • BluetoothHealthAppConfiguration

BluetoothAdapter類

通過BluetoothAdapter類的幫助,我們可以執行一些基本任務,如啟動設備發現,查詢已配對的設備列表,創建BluetoothServerSocket實例以偵聽連接請求等。愛掏網 - it200.com

BluetoothAdapter類的常量

BluetoothAdapter類提供了許多常量。愛掏網 - 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的實例。愛掏網 - it200.com
  • boolean enable() 如果藍牙適配器已禁用,則啟用藍牙適配器。愛掏網 - it200.com
  • boolean isEnabled() 如果藍牙適配器已啟用,則返回true。愛掏網 - it200.com
  • boolean disable() 如果藍牙適配器已啟用,則禁用藍牙適配器。愛掏網 - it200.com
  • String getName() 返回藍牙適配器的名稱。愛掏網 - it200.com
  • boolean setName(String name) 更改藍牙名稱。愛掏網 - it200.com
  • int getState() 返回本地藍牙適配器的當前狀態。愛掏網 - it200.com
  • Set <BluetoothDevice> getBondedDevices() 返回一組已配對的BluetoothDevice對象。愛掏網 - it200.com
  • boolean startDiscovery() 啟動發現過程。愛掏網 - it200.com

Android藍牙示例:以編程方式啟用、禁用和使藍牙可被發現

你只需要寫幾行代碼,就可以啟用或禁用藍牙。愛掏網 - it200.com

activity_main.xml

從面板中拖動一個文本視圖和三個按鈕,現在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>

提供權限

您需要在AndroidManifest.xml文件中提供以下權限。愛掏網 - it200.com

    <uses-permission android:name="android.permission.BLUETOOTH" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

下面是AndroidManifest.xml文件的完整代碼。愛掏網 - 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類

讓我們編寫代碼來啟用、禁用和使藍牙可發現。愛掏網 - 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;
    }

}

聲明:所有內容來自互聯網搜索結果,不保證100%準確性,僅供參考。如若本站內容侵犯了原著者的合法權益,可聯系我們進行處理。
發表評論
更多 網友評論0 條評論)
暫無評論

返回頂部

主站蜘蛛池模板: 日本高清天码一区在线播放| 香港三级电影在线观看| 污视频网站免费观看| 天天爽亚洲中文字幕| 免费超爽大片黄| www.好吊妞| 男人日女人app| 天堂√在线中文最新版8| 伊人网综合在线视频| av无码精品一区二区三区四区| 狼群资源网在线视频免费观看| 天美传媒一区二区三区| 人人鲁人人莫人人爱精品| JLZZJLZZ全部女高潮| 欧美精品第欧美第12页| 国产精品天干天干综合网| 亚洲中文字幕av每天更新| 国产成人精品怡红院| 日本大乳高潮视频在线观看| 国产一级不卡毛片| 中国内地毛片免费高清| 男朋友想吻我腿中间那个部位| 女人zozozo与禽交| 亚洲欧美另类一区| 香蕉在线精品视频在线观看2| 极品粉嫩小泬白浆20p| 国产午夜在线观看| 中文字幕日韩专区| 看成年女人免费午夜视频| 在线一区免费播放| 亚洲av综合色区无码一区爱av| 香蕉eeww99国产在线观看| 把胡萝卜立着自己坐上去| 免费少妇a级毛片| 91久久亚洲国产成人精品性色| 最近高清日本免费| 国产一卡二卡四卡免费| mm131美女做爽爽爱视频| 欧美成人一区二区三区在线观看 | 老熟妇高潮一区二区三区| 孩交精品xxxx视频视频|