Androidが開発したBluetooth--ネイティブBluetoothデバイスの可視性を変更し、周辺で使用可能なBluetoothデバイスをスキャン

7471 ワード

一.ネイティブBluetoothデバイスの表示を変更する
二.周辺で使用可能なBluetoothデバイスをスキャン
 
一.インベントリファイルxml:
が必要です.
 
二.レイアウトファイル:main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
	<TextView  
    	android:layout_width="fill_parent" 
    	android:layout_height="wrap_content" 
    	android:text="@string/hello"
    	/>
    <Button 
    	android:id="@+id/discoverButton"
    	android:layout_width="fill_parent"
    	android:layout_height="wrap_content"
    	android:text="     "/>
    <Button 
    	android:id="@+id/scanButton"
    	android:layout_width="fill_parent"
    	android:layout_height="wrap_content"
    	android:text="    "/>
</LinearLayout>

  
三.MainActivity:
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.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {
    private Button discoverButton = null;
    private Button scanButton = null;
    private BluetoothAdapter adapter = null;
    private BluetoothReceiver bluetoothReceiver = null;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        adapter = BluetoothAdapter.getDefaultAdapter();
        
        discoverButton = (Button)findViewById(R.id.discoverButton);
        scanButton = (Button)findViewById(R.id.scanButton);
        //          
        discoverButton.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View view) {
            Intent discoverIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);

//       ,500      (  : ),    300    300
discoverIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION,500);
startActivity(discoverIntent);
            }
        });
        
        scanButton.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View v) {
        //          ,                 ,        BroadcastReceiver     
                adapter.startDiscovery();
            }
        });
        
        //       filter
        IntentFilter intentFilter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
        //         receiver
        bluetoothReceiver = new BluetoothReceiver ();
        //       
        registerReceiver(bluetoothReceiver,intentFilter);
            
    }
    
    private class BluetoothReceiver extends BroadcastReceiver{
        @Override
        public void onReceive(Context context, Intent intent) {
            //            
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            System.out.println(device.getAddress());
        }
        
    }
}