Я пытаюсь подключить несколько действий.
(Источник списка устройств. java и LedControl. java: https://github.com/danz1ka19/Android-App-HC05-Arduino/tree/master/app/src/main/java/com/example/danyal/bluetoothhc05)
Вот мой код:
DeviceList. java
public class DeviceList extends AppCompatActivity {
Button btnPaired;
ListView devicelist;
private BluetoothAdapter myBluetooth = null;
private Set<BluetoothDevice> pairedDevices;
public static String EXTRA_ADDRESS = "device_address";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_device_list);
btnPaired = (Button) findViewById(R.id.button);
devicelist = (ListView) findViewById(R.id.listView);
myBluetooth = BluetoothAdapter.getDefaultAdapter();
if ( myBluetooth==null ) {
Toast.makeText(getApplicationContext(), "Bluetooth device not available", Toast.LENGTH_LONG).show();
finish();
} else if ( !myBluetooth.isEnabled() ) {
Intent turnBTon = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(turnBTon, 1);
}
btnPaired.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
pairedDevicesList();
}
});
}
private void pairedDevicesList () {
pairedDevices = myBluetooth.getBondedDevices();
ArrayList list = new ArrayList();
if ( pairedDevices.size() > 0 ) {
for ( BluetoothDevice bt : pairedDevices ) {
list.add(bt.getName().toString() + "\n" + bt.getAddress().toString());
}
} else {
Toast.makeText(getApplicationContext(), "No Paired Bluetooth Devices Found.", Toast.LENGTH_LONG).show();
}
final ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, list);
devicelist.setAdapter(adapter);
devicelist.setOnItemClickListener(myListClickListener);
}
private AdapterView.OnItemClickListener myListClickListener = new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String info = ((TextView) view).getText().toString();
String address = info.substring(info.length()-17);
Intent i = new Intent(DeviceList.this, ledControl.class);
i.putExtra(EXTRA_ADDRESS, address);
startActivity(i);
}
};
LedControl. java
public class ledControl extends AppCompatActivity {
Button btn1, btn2, btn3, btn4, btn5, btnDis;
String address = null;
TextView lumn;
private ProgressDialog progress;
BluetoothAdapter myBluetooth = null;
BluetoothSocket btSocket = null;
private boolean isBtConnected = false;
static final UUID myUUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent newint = getIntent();
address = newint.getStringExtra(DeviceList.EXTRA_ADDRESS);
setContentView(R.layout.activity_led_control);
btn1 = (Button) findViewById(R.id.button2);
btn2 = (Button) findViewById(R.id.button3);
btn3 = (Button) findViewById(R.id.button5);
btn4 = (Button) findViewById(R.id.button6);
btn5 = (Button) findViewById(R.id.button7);
btnDis = (Button) findViewById(R.id.button4);
lumn = (TextView) findViewById(R.id.textView2);
new ConnectBT().execute();
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(ledControl.this, test.class);
i.putExtra(DeviceList.EXTRA_ADDRESS, address);
startActivity(i);
}
});
}
private void sendSignal ( String number ) {
if ( btSocket != null ) {
try {
btSocket.getOutputStream().write(number.toString().getBytes());
} catch (IOException e) {
msg("Error");
}
}
}
private void Disconnect () {
if ( btSocket!=null ) {
try {
btSocket.close();
} catch(IOException e) {
msg("Error");
}
}
finish();
}
private void msg (String s) {
Toast.makeText(getApplicationContext(), s, Toast.LENGTH_LONG).show();
}
private class ConnectBT extends AsyncTask<Void, Void, Void> {
private boolean ConnectSuccess = true;
@Override
protected void onPreExecute () {
progress = ProgressDialog.show(ledControl.this, "Connecting...", "Please Wait!!!");
}
@Override
protected Void doInBackground (Void... devices) {
try {
if ( btSocket==null || !isBtConnected ) {
myBluetooth = BluetoothAdapter.getDefaultAdapter();
BluetoothDevice dispositivo = myBluetooth.getRemoteDevice(address);
btSocket = dispositivo.createInsecureRfcommSocketToServiceRecord(myUUID);
BluetoothAdapter.getDefaultAdapter().cancelDiscovery();
btSocket.connect();
}
} catch (IOException e) {
ConnectSuccess = false;
}
return null;
}
@Override
protected void onPostExecute (Void result) {
super.onPostExecute(result);
if (!ConnectSuccess) {
msg("Connection Failed. Is it a SPP Bluetooth? Try again.");
finish();
} else {
msg("Connected");
isBtConnected = true;
}
progress.dismiss();
}
}}
Пока все хорошо. Будучи новичком, я пытался заставить Bluetooth работать над новым видом деятельности. Но когда я вхожу в этот тест. java активность. Я возвращаюсь к первому упражнению «DeviseList. java» (или приложение cra sh).
Test. java
public class test extends AppCompatActivity {
String address = null;
private ProgressDialog progress;
BluetoothAdapter myBluetooth = null;
BluetoothSocket btSocket = null;
private boolean isBtConnected = false;
static final UUID myUUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
private TextView connect;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent newint = getIntent();
address = newint.getStringExtra(DeviceList.EXTRA_ADDRESS);
setContentView(R.layout.activity_test);
connect = (TextView) findViewById(R.id.textView4);
new ConnectBT().execute();
}
private void msg (String s) {
Toast.makeText(getApplicationContext(), s, Toast.LENGTH_LONG).show();
}
private class ConnectBT extends AsyncTask<Void, Void, Void> {
private boolean ConnectSuccess = true;
@Override
protected Void doInBackground (Void... devices) {
try {
if ( btSocket==null || !isBtConnected ) {
myBluetooth = BluetoothAdapter.getDefaultAdapter();
BluetoothDevice dispositivo = myBluetooth.getRemoteDevice(address);
btSocket = dispositivo.createInsecureRfcommSocketToServiceRecord(myUUID);
BluetoothAdapter.getDefaultAdapter().cancelDiscovery();
btSocket.connect();
}
} catch (IOException e) {
ConnectSuccess = false;
}
return null;
}
@Override
protected void onPostExecute (Void result) {
super.onPostExecute(result);
if (!ConnectSuccess) {
msg("Connection Failed. Is it a SPP Bluetooth? Try again.");
finish();
} else {
msg("Connected");
isBtConnected = true;
}
progress.dismiss();
}
}}
Спасибо Вам за помощь.