Android藍牙列出已配對設備示例含代碼

Android藍牙列出已配對設備示例

BluetoothAdapter類的getBoundedDevices()方法提供了一個包含所有已配對或綁定的藍牙設備列表的集合。愛掏網 - it200.com

在這個示例中,我們正在檢查藍牙是否關閉,如果是,則打開它并列出所有已配對的設備。愛掏網 - 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:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="18dp"
        android:layout_marginTop="61dp"
        android:text="Showing Paired Devices:" />

</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.bluetoothshowpaired"
    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.bluetoothshowpaired.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.bluetoothshowpaired;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import java.util.Set;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.widget.TextView;

public class MainActivity extends Activity {
      TextView textview1;
      private static final int REQUEST_ENABLE_BT = 1;
      BluetoothAdapter btAdapter; 

      /** Called when the activity is first created. */
      @Override
      public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textview1 = (TextView) findViewById(R.id.textView1);

        // Getting the Bluetooth adapter
        btAdapter = BluetoothAdapter.getDefaultAdapter();
        textview1.append("\nAdapter: " + btAdapter);

        CheckBluetoothState();
      }

      /* It is called when an activity completes.*/
      @Override
      protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_ENABLE_BT) {
          CheckBluetoothState();
        }
      }

      @Override
      protected void onDestroy() {
        super.onDestroy();
      }

      private void CheckBluetoothState() {
        // Checks for the Bluetooth support and then makes sure it is turned on
        // If it isn't turned on, request to turn it on
        // List paired devices
        if(btAdapter==null) { 
          textview1.append("\nBluetooth NOT supported. Aborting.");
          return;
        } else {
          if (btAdapter.isEnabled()) {
            textview1.append("\nBluetooth is enabled...");

            // Listing paired devices
            textview1.append("\nPaired Devices are:");
            Set<BluetoothDevice> devices = btAdapter.getBondedDevices();
            for (BluetoothDevice device : devices) {
              textview1.append("\n  Device: " + device.getName() + ", " + device);
            }
          } else {
            //Prompt user to turn on Bluetooth
            Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
          }
        }
      }


    @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亚洲精品| 黑人vs亚洲人在线播放| ol丝袜高跟秘书在线观看视频| avav在线看| 精品72久久久久久久中文字幕| 暖暖日本在线视频| 国产麻传媒精品国产AV| 国产精品久久久久…| 免费精品一区二区三区在线观看| 久久精品一区二区三区四区| 8天堂资源在线官网| 精品久久久无码中文字幕| 日韩一级黄色影片| 国产精品午夜小视频观看| 偷天宝鉴在线观看| 中文字幕一区二区三区人妻少妇| 黑人巨大精品欧美一区二区免费| 日韩色图在线观看| 天天干天天干天天干天天干天天干| 国产av无码专区亚洲av毛片搜| 久久精品天天中文字幕人妻| 探花视频在线看视频| 欧美精品一区二区三区视频| 奇米影视777me| 公和我做好爽添厨房| 中文字幕.com| 老鸭窝二区三区在线播放| 日本视频网站在线www色| 国产成人在线网址| 亚洲va无码va在线va天堂| 91国内揄拍国内精品对白| 欧美亚洲综合另类| 国产精品免费精品自在线观看| 亚洲av丰满熟妇在线播放| 香蕉视频在线免费看| 欧美日韩国产三上悠亚在线看| 国外成人免费高清激情视频| 国产综合久久久久久鬼色| 免费又黄又硬又爽大片| 一线在线观看全集免费高清中文| 色噜噜狠狠狠狠色综合久一 |