Reputation: 2345
I have a function that connects to a device (a Bluetooth credit card machine in this case) that looks like the following:
private void pinPar(final String name, final String address) {
MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
pinpadSelected = new PinpadObject(name, address, false);
BluetoothConnectionProvider bluetoothConnectionProvider = new BluetoothConnectionProvider(MainActivity.this, pinpadSelected);
bluetoothConnectionProvider.setDialogMessage("Connecting to pinpad");
bluetoothConnectionProvider.setWorkInBackground(false);
bluetoothConnectionProvider.setConnectionCallback(new StoneCallbackInterface() {
@Override
public void onSuccess() {
Toast.makeText(getApplicationContext(), "Pinpad connected", Toast.LENGTH_SHORT).show();
out.println("Connected to " + name + " at " + address);
}
@Override
public void onError() {
Toast.makeText(getApplicationContext(), "Connection failed", Toast.LENGTH_SHORT).show();
out.println("Failed connecting to "+ name + " at " + address);
}
}
);
bluetoothConnectionProvider.execute();
}
});
}
I was looking to make an analogous function, pinUnpar
that would simply close that connection, but bluetoothConnectionProvider
has no method close()
or something of the kind. How can I achieve this?
Upvotes: 0
Views: 503
Reputation: 777
Alrighty, I haven't dealt with bluetooth on android in a while but here goes. There are many different ways of connecting devices using bluetooth, however one simple way I particularly like, because it doesn't require scanning for the device you wish to connect to, and doesn't require pairing either. It is as follows:
first off you need a common UUID that both your client and server know, since your server in this case is the bluetooth credit card machine, you need to find out what the UUID it uses for the connection is (Shouldn't be too hard, if it's not written in the manual of the machine, then you can detect it yourself using a laptop).
Client code :-
BluetoothAdapter adapter;
adapter= BluetoothAdapter.getDefaultAdapter();
BluetoothDevice device;
device= adapter.getRemoteDevice(serverAddress); //address here would be the address value
//passed to your function
BluetoothSocket socket= device.createInsecureRfcommSocketToServiceRecord(uuid);
//here uuid is the UUID the device uses as mentioned perviously
socket.connect();
OutputStream ouput=socket.getOutputStream();
InputStream input=socket.getInputStream();
And just like that, you have a connection to your machine, which you can write anything and read anything on. I assume you're not programming the credit card machine, so I omitted the corresponding server code.
Since this code uses simple streams and sockets, it's very easy to close, just as it is easy to open.
Edit:-
This uses android API only for the BT connection, and PLEASE NOTE that this code uses an INSECURE rfcomm, which means it's vulnerable to MITMA and other such attacks. If you don't wish for that you can instead replace
device.createInsecureRfcommSocketToServiceRecord(uuid)
with
device.createRfcommSocketToServiceRecord(uuid);
Upvotes: 1