Example 1: arduino ble led dimmer code
private void Disconnect(){
if (btSocket!=null)
{
try
{
btSocket.close();
}
catch (IOException e)
{ msg("Error");}
}
finish();
Example 2: arduino ble led dimmer code
private void msg(String s) { Toast.makeText(getApplicationContext(),s,Toast.LENGTH_LONG).show(); }
Example 3: arduino ble led dimmer code
private class ConnectBT extends AsyncTask<Void, Void, Void>
private boolean ConnectSuccess = true;
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();
}
}
Example 4: arduino ble led dimmer code
btnPaired.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { pairedDevicesList();
Example 5: arduino ble led dimmer code
import android.bluetooth.BluetoothSocket;
import android.content.Intent;import android.view.View;import android.widget.Button;import android.widget.SeekBar;import android.widget.TextView;import android.widget.Toast;import android.app.ProgressDialog;import android.bluetooth.BluetoothAdapter;import android.bluetooth.BluetoothDevice;import android.os.AsyncTask;import java.io.IOException;import java.util.UUID;
Example 6: arduino ble led dimmer code
myBluetooth = BluetoothAdapter.getDefaultAdapter();if(myBluetooth == null) {
else { if (myBluetooth.isEnabled()) { } else {
Intent turnBTon = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(turnBTon,1); }}
Example 7: arduino ble led dimmer code
private AdapterView.OnItemClickListener myListClickListener = new AdapterView.OnItemClickListener()
{
public void onItemClick (AdapterView av, View v, int arg2, long arg3)
{
String info = ((TextView) v).getText().toString();
String address = info.substring(info.length() - 17);
Intent i = new Intent(DeviceList.this, ledControl.class);
i.putExtra(EXTRA_ADDRESS, address);
startActivity(i);
}
};
Example 8: arduino ble led dimmer code
private BluetoothAdapter myBluetooth = null;private Set pairedDevices;
Example 9: arduino ble led dimmer code
private void pairedDevicesList(){
pairedDevices = myBluetooth.getBondedDevices();
ArrayList list = new ArrayList(); if (pairedDevices.size()>0)
{
for(BluetoothDevice bt : pairedDevices)
{
list.add(bt.getName() + "\n" + bt.getAddress());
}
}
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);
Example 10: arduino ble led dimmer code
btnPaired = (Button)findViewById(R.id.button);devicelist = (ListView)findViewById(R.id.listView);