電話でペアリングされたすべてのBluetoothデバイスを選択してArrayAdapterに入力するこのアプリケーションを実行してから、範囲内のBluetoothデバイスを検索し、arrayAdapterも更新します。しかし、このアプリを実行しようとするとエラーが発生します。
public class MainActivity extends Activity implements OnItemClickListener {
ListView JlistView1;
BluetoothAdapter btAdpter;
ArrayAdapter<String> arrayAdapter;
Set<BluetoothDevice> devicesArray;
ArrayList<String> pairedDevices;
ArrayList<BluetoothDevice> devices;
IntentFilter filter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setUP();
bluetoothStatus();
gettingPairedDevices();
startingDiscovering();
searchDevicesInrange();
}
public void setUP() {
// setting up user interface
pairedDevices = new ArrayList<String>();
devices = new ArrayList<BluetoothDevice>();
JlistView1 = (ListView) findViewById(R.id.listView1);
btAdpter = BluetoothAdapter.getDefaultAdapter();
arrayAdapter = new ArrayAdapter<String>(getApplicationContext(),
android.R.layout.simple_list_item_1, pairedDevices);
JlistView1.setAdapter(arrayAdapter);
JlistView1.setOnItemClickListener(this);
}
public void bluetoothStatus() {
if (btAdpter == null) {
// there is not driver
Toast.makeText(getApplicationContext(),
"There is no bluetooth adapter", 0).show();
finish();
} else {
if (btAdpter.isEnabled()) {
Toast.makeText(getApplicationContext(), "Bluetooth is enabled",
Toast.LENGTH_SHORT).show();
} else {
enablingBluetooth();
}
}
}
public void enablingBluetooth() {
Intent filter = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(new Intent(filter), 0);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_CANCELED) {
Toast.makeText(getApplicationContext(),
"Bluetooth must be enabled to continue", Toast.LENGTH_SHORT)
.show();
finish();
}
}
public void gettingPairedDevices() {
devicesArray = btAdpter.getBondedDevices();
if (devicesArray.size() > 0) {
for (BluetoothDevice device : devicesArray) {
pairedDevices.add(device.getName());
}
}
}
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
if (btAdpter.isDiscovering()) {
btAdpter.cancelDiscovery();
}
String device = pairedDevices.get(arg2).toString();
Toast.makeText(getApplicationContext(), "Your selected " + device,
Toast.LENGTH_SHORT).show();
}
public void searchDevicesInrange() {
filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(RangReciever, filter);
filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
registerReceiver(RangReciever, filter);
filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
registerReceiver(RangReciever, filter);
filter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
registerReceiver(RangReciever, filter);
}
BroadcastReceiver RangReciever = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
String action = intent.getAction();
// if devices found
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
BluetoothDevice device = intent
.getParcelableExtra(BluetoothDevice.ACTION_FOUND);
devices.add(device);
pairedDevices.add(device.getName() + " new");
} else if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) {
} else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED
.equals(action)) {
} else if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) {
if (btAdpter.getState() == btAdpter.STATE_OFF) {// turn on
// bluetooth
// again
enablingBluetooth();
}
}
}
};
public void startingDiscovering() {
btAdpter.cancelDiscovery();
btAdpter.startDiscovery();
}
Logcat 出力
01-09 14:00:57.430: E/ActivityThread(1463): Activity com.jingo.bluetoothjingo.MainActivity has leaked IntentReceiver com.jingo.bluetoothjingo.MainActivity$1@4051f108 that was originally registered here. Are you missing a call to unregisterReceiver()?
01-09 14:00:57.430: E/ActivityThread(1463): android.app.IntentReceiverLeaked: Activity com.jingo.bluetoothjingo.MainActivity has leaked IntentReceiver com.jingo.bluetoothjingo.MainActivity$1@4051f108 that was originally registered here. Are you missing a call to unregisterReceiver()?
01-09 14:00:57.430: E/ActivityThread(1463): at android.app.LoadedApk$ReceiverDispatcher.<init>(LoadedApk.java:799)
01-09 14:00:57.430: E/ActivityThread(1463): at android.app.LoadedApk.getReceiverDispatcher(LoadedApk.java:575)
01-09 14:00:57.430: E/ActivityThread(1463): at android.app.ContextImpl.registerReceiverInternal(ContextImpl.java:857)
01-09 14:00:57.430: E/ActivityThread(1463): at android.app.ContextImpl.registerReceiver(ContextImpl.java:844)
01-09 14:00:57.430: E/ActivityThread(1463): at android.app.ContextImpl.registerReceiver(ContextImpl.java:838)
01-09 14:00:57.430: E/ActivityThread(1463): at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:318)
01-09 14:00:57.430: E/ActivityThread(1463): at com.jingo.bluetoothjingo.MainActivity.searchDevicesInrange(MainActivity.java:114)
01-09 14:00:57.430: E/ActivityThread(1463): at com.jingo.bluetoothjingo.MainActivity.onCreate(MainActivity.java:40)
01-09 14:00:57.430: E/ActivityThread(1463): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1072)
01-09 14:00:57.430: E/ActivityThread(1463): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1785)
01-09 14:00:57.430: E/ActivityThread(1463): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1842)
01-09 14:00:57.430: E/ActivityThread(1463): at android.app.ActivityThread.access$1500(ActivityThread.java:132)
01-09 14:00:57.430: E/ActivityThread(1463): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1038)
01-09 14:00:57.430: E/ActivityThread(1463): at android.os.Handler.dispatchMessage(Handler.java:99)
01-09 14:00:57.430: E/ActivityThread(1463): at android.os.Looper.loop(Looper.java:150)
01-09 14:00:57.430: E/ActivityThread(1463): at android.app.ActivityThread.main(ActivityThread.java:4263)
01-09 14:00:57.430: E/ActivityThread(1463): at java.lang.reflect.Method.invokeNative(Native Method)
01-09 14:00:57.430: E/ActivityThread(1463): at java.lang.reflect.Method.invoke(Method.java:507)
01-09 14:00:57.430: E/ActivityThread(1463): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-09 14:00:57.430: E/ActivityThread(1463): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-09 14:00:57.430: E/ActivityThread(1463): at dalvik.system.NativeStart.main(Native Method)