Android Bluetoothテスト

8425 ワード

1.権限の追加

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

2.main.xml

<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" tools:context="com.example.bluetoothtest.MainActivity" >

    <ListView  android:id="@+id/listView1" android:layout_width="match_parent" android:layout_height="match_parent" >
    </ListView>

</RelativeLayout>

3.MainActivity.java

package com.example.bluetoothtest;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

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.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends Activity {
    private BluetoothAdapter bluetoothAdapter = BluetoothAdapter
            .getDefaultAdapter();
    private List<BluetoothDevice> deviceList = new ArrayList<BluetoothDevice>();
    private int REQUEST_ENABLE_BT = 1;

    private ListView listView;
    private ArrayAdapter<String> adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        listView = (ListView)findViewById(R.id.listView1);
        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);
        listView.setAdapter(adapter);

        if (bluetoothAdapter == null) {
            Toast.makeText(getApplicationContext(), "bluetooth not exist!",
                    Toast.LENGTH_SHORT).show();
            finish();
        } else if (!bluetoothAdapter.isEnabled()) {
            // Show a system activity that allows the user to turn on Bluetooth
            Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(intent, REQUEST_ENABLE_BT);
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        //  
        Intent discoverIntent = new Intent(
                BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
        //  , 10s
        discoverIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION,
                10);
        startActivity(discoverIntent);

        IntentFilter discoverFilter = new IntentFilter(
                BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
        registerReceiver(discover_R, discoverFilter);
        IntentFilter findFilter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
        registerReceiver(find_R, findFilter);

        bluetoothAdapter.startDiscovery();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (bluetoothAdapter != null) {
            bluetoothAdapter.cancelDiscovery();
            bluetoothAdapter.disable();
        }
    }

    // ACTION_DISCOVERY_FINISHED, 
    // getName() ,“ ” 
    BroadcastReceiver discover_R = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            getName();
            unregisterReceiver(find_R);
            unregisterReceiver(this);
        }
    };

    // ACTION_FOUND, ( + )
    BroadcastReceiver find_R = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            deviceList.add(device);
            adapter.add(device.getName());
        }
    };

    private void getName() {
        Set<BluetoothDevice> pairedDevices = bluetoothAdapter.getBondedDevices();
        if(pairedDevices.size() > 0){
            for(BluetoothDevice device : pairedDevices){
                deviceList.add(device);
                adapter.add(device.getName()+" !");
            }
        }
    }
}

注意点:getBondedDevicesを実行するのに少し時間がかかりますが、最初は知りませんでしたが、「ペアリング済み」はずっと現れず、間違いだと思っていました.あとでトイレに行って帰ってきて気づいたらあった!!!気まずい!