問題があります。Bluetooth シールドとの接続を確立できません。別のアプリケーションを使用していて、デバイスが動作している場合は、接続されているライトで示されますが、このアプリでは発生しません。tmp = mmDevice.createRfcommSocketToServiceRecord (MY_UUID); 不正なソケット取得でない場合、このメソッドが mmSocket.connect () と呼ばれるとアプリがクラッシュします。助けてください私はこれが初めてです=)
import java.io.IOException;
import java.util.UUID;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
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.view.View.OnLongClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;
public class ConfigView extends Activity implements OnClickListener, OnLongClickListener , OnItemSelectedListener {
Button bnt1;
Spinner listDevicesFound;
public BluetoothAdapter mBluetoothAdapter;
private BluetoothSocket mmSocket;
private BluetoothDevice mmDevice;
private final UUID MY_UUID = UUID.fromString("6170d0f0-5bc3-11e2-bcfd-0800200c9a66");
ArrayAdapter<String> btArrayAdapter;
String deviceToConnect;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.configview);
bnt1 =(Button) findViewById(R.id.button1);
bnt1.setOnClickListener(this);
bnt1.setOnLongClickListener(this);
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
listDevicesFound = (Spinner) findViewById(R.id.spinner1);
listDevicesFound.setOnItemSelectedListener(this);
btArrayAdapter = new ArrayAdapter<String>(ConfigView.this, android.R.layout.simple_list_item_1);
listDevicesFound.setAdapter(btArrayAdapter);
registerReceiver(ActionFoundReceiver,new IntentFilter(BluetoothDevice.ACTION_FOUND));
//ScanDevices
btArrayAdapter.clear();
mBluetoothAdapter.startDiscovery();
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
unregisterReceiver(ActionFoundReceiver);
}
//Buttons Listeners
@Override
public void onClick(View v) {
if (v.getId() == R.id.button1)
{
String address = deviceToConnect.substring(deviceToConnect.length() - 17);
Toast.makeText(getApplicationContext(), address, Toast.LENGTH_SHORT).show();
mmDevice = mBluetoothAdapter.getRemoteDevice(address);
BluetoothSocket tmp = null;
// Get a BluetoothSocket to connect with the given BluetoothDevice
try {
// MY_UUID is the app's UUID string, also used by the server code
tmp = mmDevice.createRfcommSocketToServiceRecord(MY_UUID);
} catch (IOException e) { }
mmSocket = tmp;
}
mBluetoothAdapter.cancelDiscovery();
try {
// Connect the device through the socket. This will block
// until it succeeds or throws an exception
mmSocket.connect();
} catch (IOException connectException) {
// Unable to connect; close the socket and get out
try {
mmSocket.close();
} catch (IOException closeException) { }
return;
}
}
@Override
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
if (v.getId() == R.id.button1)
{
try {
mmSocket.close();
} catch (IOException e) { }
}
return true;
}
private final BroadcastReceiver ActionFoundReceiver = new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
String action = intent.getAction();
if(BluetoothDevice.ACTION_FOUND.equals(action)) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
btArrayAdapter.add(device.getName() + "\n" + device.getAddress());
btArrayAdapter.notifyDataSetChanged();
}
}};
public void onItemSelected(AdapterView<?> arg0, View v, int position,
long id) {
// TODO Auto-generated method stub
deviceToConnect = btArrayAdapter.getItem(position);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
deviceToConnect = "";
}
}