AndroidのBluetooth操作(二)
1、本ブルートゥースデバイスの可視性を変更する
2、周辺で使用可能なブルートゥースデバイスをスキャンする
手順:
1、androidManifest.xmlに次のコードを追加
2、レイアウトファイルmain.xmlに2つのボタンを追加
3、Activityファイル
2、周辺で使用可能なブルートゥースデバイスをスキャンする
手順:
1、androidManifest.xmlに次のコードを追加
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
2、レイアウトファイルmain.xmlに2つのボタンを追加
<?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/discoverId"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=" "
/>
<Button
android:id="@+id/scanId"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=" "
/>
</LinearLayout>
3、Activityファイル
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 TestBluetooth02Activity extends Activity {
/** Called when the activity is first created. */
private Button discoverButton = null;
private Button scanButton = null;
private BluetoothReceiver bluetoothReceiver = null;
private BluetoothAdapter bluetoothAdapter = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
discoverButton = (Button)findViewById(R.id.discoverId);
discoverButton.setOnClickListener(new DiscoverButtonListener());
// IntentFilter , action BluetoothDevice.ACTION_FOUND
IntentFilter intentFilter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
// BluetoothReceive
bluetoothReceiver = new BluetoothReceiver();
//
registerReceiver(bluetoothReceiver,intentFilter);
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
scanButton = (Button)findViewById(R.id.scanId);
scanButton.setOnClickListener(new ScanButtonListener());
}
private class DiscoverButtonListener implements OnClickListener
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// Intent , Action BluetoothAdapter.ACTION_REQEST_DISCOVERABLE
Intent discoverIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
// Intent ,
discoverIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 500);
startActivity(discoverIntent);
}
}
private class BluetoothReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
String action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
// Intent ,
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
System.out.println(device.getAddress());
}
}
}
private class ScanButtonListener implements OnClickListener
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
bluetoothAdapter.startDiscovery();
}
}
/* protected void onDestroy
{
unregisterReceiver(bluetoothReceiver);
super.onDestroy();
}*/
}