2013/11/03

Android Find Bluetooth Device

要注意撰寫版本與官網API版本相同

AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.cy.findbluetooth"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="10"
        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="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>



Layout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" >
    </ListView>

</RelativeLayout>


Code:
package com.example.bluetooth;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends Activity {
 static BluetoothAdapter bluetoothAdapter;
 ListView listView;
 ArrayAdapter<String> arrayAdapter;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  listView = (ListView) findViewById(R.id.listView1);
  bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
  listView = (ListView) findViewById(R.id.listView1);

  // Not device
  if (bluetoothAdapter == null) {
   // Messagebox
   Toast.makeText(this, "你沒有藍芽裝置!", Toast.LENGTH_LONG).show();

   // Exit App
   finish();
  }
  arrayAdapter = new ArrayAdapter<String>(this,
    android.R.layout.simple_list_item_1);
  listView.setAdapter(arrayAdapter);

  // Open bluetooth
  if (!bluetoothAdapter.isEnabled()) {
   Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
   startActivity(intent);

  }

  IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
  registerReceiver(broadcastReceiver, filter);

  bluetoothAdapter.startDiscovery();
 }

 final BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {

  @Override
  public void onReceive(Context context, Intent intent) {
   String action = intent.getAction();
   if (action.equals(BluetoothDevice.ACTION_FOUND)) {
    Log.i("ifno", "find");
    BluetoothDevice device = intent
      .getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
    arrayAdapter.add(device.getName() + ", " + device.getAddress());
   }
  }
 };
}




參考資料:
http://developer.android.com/guide/topics/connectivity/bluetooth.html
http://developer.android.com/guide/topics/connectivity/bluetooth-le.html
http://developer.android.com/reference/android/bluetooth/BluetoothAdapter.html#startLeScan(android.bluetooth.BluetoothAdapter.LeScanCallback)
http://developer.android.com/reference/android/bluetooth/BluetoothDevice.html
http://developer.android.com/reference/android/bluetooth/BluetoothAdapter.html
http://developer.android.com/reference/android/content/Context.html#BLUETOOTH_SERVICE
http://developer.android.com/guide/topics/connectivity/bluetooth.html#FindingDevices
http://developer.android.com/reference/android/bluetooth/BluetoothAdapter.html#getBondedDevices()
http://stackoverflow.com/questions/7924825/discovering-available-bluetooth-devices-in-android