Connect to a RFID reader using android bluetooth
I've been trying to develop an app which I could integrate with a RFID reader (I'm using a 1128 UHF RFID Reader). I've recently trying to learn how to use Bluetooth in android studio, i have create a simple app where I could turn on Bluetooth, find devices and pair with them. But I couldn't pair with the RFID reader. I'm new to android programming so I'm not sure how to do it properly.
Here is my code :
package com.siscaproject.sisca.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.SwitchCompat;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import com.siscaproject.sisca.Adapter.BluetoothListAdapter;
import com.siscaproject.sisca.R;
import java.util.ArrayList;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnCheckedChanged;
public class HomeActivity extends AppCompatActivity
private static final String TAG = "HomeActivity";
private BluetoothAdapter mBluetoothAdapter;
private BroadcastReceiver mBroadcastReceiver1 = new BroadcastReceiver()
@Override
public void onReceive(Context context, Intent intent)
String action = intent.getAction();
if(action.equals(mBluetoothAdapter.ACTION_STATE_CHANGED))
final int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, mBluetoothAdapter.ERROR);
switch(state)
case BluetoothAdapter.STATE_OFF:
Log.d(TAG, "onReceive: STATE OFF");
break;
case BluetoothAdapter.STATE_TURNING_OFF:
Log.d(TAG, "onReceive: STATE TURNING OFF");
break;
case BluetoothAdapter.STATE_ON:
Log.d(TAG, "onReceive: STATE ON");
break;
case BluetoothAdapter.STATE_TURNING_ON:
Log.d(TAG, "onReceive: STATE TURNING ON");
break;
;
private BroadcastReceiver mBroadcastReceiver2 = new BroadcastReceiver()
@Override
public void onReceive(Context context, Intent intent)
final String action = intent.getAction();
Log.d(TAG, "onReceive: ACTION FOUND");
if(action.equals(BluetoothDevice.ACTION_FOUND))
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
// Yang dimasukin ke arraylist cuma BluetoothClass 1f00
if(device.getBluetoothClass().toString().equals("1f00"))
listDevice.add(device);
Log.d(TAG, "onReceive: " + device.getName() + ", " + device.getAddress());
rv_list_device.setHasFixedSize(true);
rv_list_device.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
BluetoothListAdapter listAdapter = new BluetoothListAdapter(listDevice,
HomeActivity.this, mBluetoothAdapter);
rv_list_device.setAdapter(listAdapter);
;
private BroadcastReceiver mBroadcastReceiver3 = new BroadcastReceiver()
@Override
public void onReceive(Context context, Intent intent)
final String action = intent.getAction();
if(action.equals(BluetoothDevice.ACTION_BOND_STATE_CHANGED))
BluetoothDevice mDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
if(mDevice.getBondState() == BluetoothDevice.BOND_BONDED)
Log.d(TAG, "BroadcastReceiver: BOND BONDED");
else if(mDevice.getBondState() == BluetoothDevice.BOND_BONDING)
Log.d(TAG, "BroadcastReceiver: BOND BONDING");
else if(mDevice.getBondState() == BluetoothDevice.BOND_NONE)
Log.d(TAG, "BroadcastReceiver: BOND NONE");
;
@BindView(R.id.sw_bluetooth) SwitchCompat sw_bluetooth;
@BindView(R.id.rv_list_device) RecyclerView rv_list_device;
@BindView(R.id.srl_list_parent) SwipeRefreshLayout srl_list_parent;
@BindView(R.id.tv_btWarning) TextView tv_btWarning;
private BluetoothListAdapter rvAdapter;
private ArrayList<BluetoothDevice> listDevice;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
ButterKnife.bind(this);
rv_list_device.setHasFixedSize(true);
rv_list_device.setLayoutManager(new LinearLayoutManager(this));
rvAdapter = new BluetoothListAdapter(listDevice, HomeActivity.this, mBluetoothAdapter);
rv_list_device.setAdapter(rvAdapter);
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
registerReceiver(mBroadcastReceiver3, filter);
listDevice = new ArrayList<>();
srl_list_parent.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener()
@Override
public void onRefresh()
refreshDevice();
new Handler().postDelayed(new Runnable()
@Override
public void run()
srl_list_parent.setRefreshing(false);
mBluetoothAdapter.cancelDiscovery();
Log.d(TAG, "onRefresh: Canceling discovery");
, 20000);
);
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
initialSwitch();
private void initialSwitch()
if(mBluetoothAdapter.isEnabled())
sw_bluetooth.setChecked(true);
srl_list_parent.setVisibility(View.VISIBLE);
tv_btWarning.setVisibility(View.GONE);
else if(!mBluetoothAdapter.isEnabled())
sw_bluetooth.setChecked(false);
srl_list_parent.setVisibility(View.GONE);
tv_btWarning.setVisibility(View.VISIBLE);
public void refreshDevice()
listDevice.clear();
Log.d(TAG, "refreshDevice: Looking for unpaired devices");
if(mBluetoothAdapter.isDiscovering())
mBluetoothAdapter.cancelDiscovery();
Log.d(TAG, "refreshDevice: Canceling discovery");
mBluetoothAdapter.startDiscovery();
IntentFilter discoverDevicesIntent = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(mBroadcastReceiver2, discoverDevicesIntent);
else if(!mBluetoothAdapter.isDiscovering())
mBluetoothAdapter.startDiscovery();
IntentFilter discoverDevicesIntent = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(mBroadcastReceiver2, discoverDevicesIntent);
@Override
protected void onDestroy()
Log.d(TAG, "onDestroy: called");
super.onDestroy();
try
if(mBroadcastReceiver1 != null)
unregisterReceiver(mBroadcastReceiver1);
mBroadcastReceiver1 = null;
catch(RuntimeException e)
Log.e(TAG, e.getMessage());
try
if(mBroadcastReceiver2 != null)
unregisterReceiver(mBroadcastReceiver2);
mBroadcastReceiver2 = null;
catch(RuntimeException e)
Log.e(TAG, e.getMessage());
try
if(mBroadcastReceiver3 != null)
unregisterReceiver(mBroadcastReceiver3);
mBroadcastReceiver3 = null;
catch(RuntimeException e)
Log.e(TAG, e.getMessage());
finish();
@OnCheckedChanged(R.id.sw_bluetooth)
public void onSwitch()
Log.d(TAG, "onSwitch: enabling/disabling bluetooth");
if(sw_bluetooth.isChecked() == true)
Log.d(TAG, "enableDisableBluetooth: enabling bluetooth");
srl_list_parent.setVisibility(View.VISIBLE);
tv_btWarning.setVisibility(View.GONE);
mBluetoothAdapter.enable();
IntentFilter BTIntent = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
registerReceiver(mBroadcastReceiver1, BTIntent);
else if(sw_bluetooth.isChecked() == false)
Log.d(TAG, "enableDisableBluetooth: disabling bluetooth");
mBluetoothAdapter.disable();
srl_list_parent.setVisibility(View.GONE);
tv_btWarning.setVisibility(View.VISIBLE);
listDevice.clear();
IntentFilter BTIntent = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
registerReceiver(mBroadcastReceiver1, BTIntent);
else if(mBluetoothAdapter == null)
Log.d(TAG, "enableDisableBluetooth: Doesn't have BT capabilities");
I could find my RFID reader device (it shows on the list), but I couldn't pair it. I've searched on how to do this but can't seem to find how to do it.
android android-bluetooth
add a comment |
I've been trying to develop an app which I could integrate with a RFID reader (I'm using a 1128 UHF RFID Reader). I've recently trying to learn how to use Bluetooth in android studio, i have create a simple app where I could turn on Bluetooth, find devices and pair with them. But I couldn't pair with the RFID reader. I'm new to android programming so I'm not sure how to do it properly.
Here is my code :
package com.siscaproject.sisca.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.SwitchCompat;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import com.siscaproject.sisca.Adapter.BluetoothListAdapter;
import com.siscaproject.sisca.R;
import java.util.ArrayList;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnCheckedChanged;
public class HomeActivity extends AppCompatActivity
private static final String TAG = "HomeActivity";
private BluetoothAdapter mBluetoothAdapter;
private BroadcastReceiver mBroadcastReceiver1 = new BroadcastReceiver()
@Override
public void onReceive(Context context, Intent intent)
String action = intent.getAction();
if(action.equals(mBluetoothAdapter.ACTION_STATE_CHANGED))
final int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, mBluetoothAdapter.ERROR);
switch(state)
case BluetoothAdapter.STATE_OFF:
Log.d(TAG, "onReceive: STATE OFF");
break;
case BluetoothAdapter.STATE_TURNING_OFF:
Log.d(TAG, "onReceive: STATE TURNING OFF");
break;
case BluetoothAdapter.STATE_ON:
Log.d(TAG, "onReceive: STATE ON");
break;
case BluetoothAdapter.STATE_TURNING_ON:
Log.d(TAG, "onReceive: STATE TURNING ON");
break;
;
private BroadcastReceiver mBroadcastReceiver2 = new BroadcastReceiver()
@Override
public void onReceive(Context context, Intent intent)
final String action = intent.getAction();
Log.d(TAG, "onReceive: ACTION FOUND");
if(action.equals(BluetoothDevice.ACTION_FOUND))
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
// Yang dimasukin ke arraylist cuma BluetoothClass 1f00
if(device.getBluetoothClass().toString().equals("1f00"))
listDevice.add(device);
Log.d(TAG, "onReceive: " + device.getName() + ", " + device.getAddress());
rv_list_device.setHasFixedSize(true);
rv_list_device.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
BluetoothListAdapter listAdapter = new BluetoothListAdapter(listDevice,
HomeActivity.this, mBluetoothAdapter);
rv_list_device.setAdapter(listAdapter);
;
private BroadcastReceiver mBroadcastReceiver3 = new BroadcastReceiver()
@Override
public void onReceive(Context context, Intent intent)
final String action = intent.getAction();
if(action.equals(BluetoothDevice.ACTION_BOND_STATE_CHANGED))
BluetoothDevice mDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
if(mDevice.getBondState() == BluetoothDevice.BOND_BONDED)
Log.d(TAG, "BroadcastReceiver: BOND BONDED");
else if(mDevice.getBondState() == BluetoothDevice.BOND_BONDING)
Log.d(TAG, "BroadcastReceiver: BOND BONDING");
else if(mDevice.getBondState() == BluetoothDevice.BOND_NONE)
Log.d(TAG, "BroadcastReceiver: BOND NONE");
;
@BindView(R.id.sw_bluetooth) SwitchCompat sw_bluetooth;
@BindView(R.id.rv_list_device) RecyclerView rv_list_device;
@BindView(R.id.srl_list_parent) SwipeRefreshLayout srl_list_parent;
@BindView(R.id.tv_btWarning) TextView tv_btWarning;
private BluetoothListAdapter rvAdapter;
private ArrayList<BluetoothDevice> listDevice;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
ButterKnife.bind(this);
rv_list_device.setHasFixedSize(true);
rv_list_device.setLayoutManager(new LinearLayoutManager(this));
rvAdapter = new BluetoothListAdapter(listDevice, HomeActivity.this, mBluetoothAdapter);
rv_list_device.setAdapter(rvAdapter);
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
registerReceiver(mBroadcastReceiver3, filter);
listDevice = new ArrayList<>();
srl_list_parent.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener()
@Override
public void onRefresh()
refreshDevice();
new Handler().postDelayed(new Runnable()
@Override
public void run()
srl_list_parent.setRefreshing(false);
mBluetoothAdapter.cancelDiscovery();
Log.d(TAG, "onRefresh: Canceling discovery");
, 20000);
);
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
initialSwitch();
private void initialSwitch()
if(mBluetoothAdapter.isEnabled())
sw_bluetooth.setChecked(true);
srl_list_parent.setVisibility(View.VISIBLE);
tv_btWarning.setVisibility(View.GONE);
else if(!mBluetoothAdapter.isEnabled())
sw_bluetooth.setChecked(false);
srl_list_parent.setVisibility(View.GONE);
tv_btWarning.setVisibility(View.VISIBLE);
public void refreshDevice()
listDevice.clear();
Log.d(TAG, "refreshDevice: Looking for unpaired devices");
if(mBluetoothAdapter.isDiscovering())
mBluetoothAdapter.cancelDiscovery();
Log.d(TAG, "refreshDevice: Canceling discovery");
mBluetoothAdapter.startDiscovery();
IntentFilter discoverDevicesIntent = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(mBroadcastReceiver2, discoverDevicesIntent);
else if(!mBluetoothAdapter.isDiscovering())
mBluetoothAdapter.startDiscovery();
IntentFilter discoverDevicesIntent = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(mBroadcastReceiver2, discoverDevicesIntent);
@Override
protected void onDestroy()
Log.d(TAG, "onDestroy: called");
super.onDestroy();
try
if(mBroadcastReceiver1 != null)
unregisterReceiver(mBroadcastReceiver1);
mBroadcastReceiver1 = null;
catch(RuntimeException e)
Log.e(TAG, e.getMessage());
try
if(mBroadcastReceiver2 != null)
unregisterReceiver(mBroadcastReceiver2);
mBroadcastReceiver2 = null;
catch(RuntimeException e)
Log.e(TAG, e.getMessage());
try
if(mBroadcastReceiver3 != null)
unregisterReceiver(mBroadcastReceiver3);
mBroadcastReceiver3 = null;
catch(RuntimeException e)
Log.e(TAG, e.getMessage());
finish();
@OnCheckedChanged(R.id.sw_bluetooth)
public void onSwitch()
Log.d(TAG, "onSwitch: enabling/disabling bluetooth");
if(sw_bluetooth.isChecked() == true)
Log.d(TAG, "enableDisableBluetooth: enabling bluetooth");
srl_list_parent.setVisibility(View.VISIBLE);
tv_btWarning.setVisibility(View.GONE);
mBluetoothAdapter.enable();
IntentFilter BTIntent = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
registerReceiver(mBroadcastReceiver1, BTIntent);
else if(sw_bluetooth.isChecked() == false)
Log.d(TAG, "enableDisableBluetooth: disabling bluetooth");
mBluetoothAdapter.disable();
srl_list_parent.setVisibility(View.GONE);
tv_btWarning.setVisibility(View.VISIBLE);
listDevice.clear();
IntentFilter BTIntent = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
registerReceiver(mBroadcastReceiver1, BTIntent);
else if(mBluetoothAdapter == null)
Log.d(TAG, "enableDisableBluetooth: Doesn't have BT capabilities");
I could find my RFID reader device (it shows on the list), but I couldn't pair it. I've searched on how to do this but can't seem to find how to do it.
android android-bluetooth
add a comment |
I've been trying to develop an app which I could integrate with a RFID reader (I'm using a 1128 UHF RFID Reader). I've recently trying to learn how to use Bluetooth in android studio, i have create a simple app where I could turn on Bluetooth, find devices and pair with them. But I couldn't pair with the RFID reader. I'm new to android programming so I'm not sure how to do it properly.
Here is my code :
package com.siscaproject.sisca.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.SwitchCompat;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import com.siscaproject.sisca.Adapter.BluetoothListAdapter;
import com.siscaproject.sisca.R;
import java.util.ArrayList;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnCheckedChanged;
public class HomeActivity extends AppCompatActivity
private static final String TAG = "HomeActivity";
private BluetoothAdapter mBluetoothAdapter;
private BroadcastReceiver mBroadcastReceiver1 = new BroadcastReceiver()
@Override
public void onReceive(Context context, Intent intent)
String action = intent.getAction();
if(action.equals(mBluetoothAdapter.ACTION_STATE_CHANGED))
final int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, mBluetoothAdapter.ERROR);
switch(state)
case BluetoothAdapter.STATE_OFF:
Log.d(TAG, "onReceive: STATE OFF");
break;
case BluetoothAdapter.STATE_TURNING_OFF:
Log.d(TAG, "onReceive: STATE TURNING OFF");
break;
case BluetoothAdapter.STATE_ON:
Log.d(TAG, "onReceive: STATE ON");
break;
case BluetoothAdapter.STATE_TURNING_ON:
Log.d(TAG, "onReceive: STATE TURNING ON");
break;
;
private BroadcastReceiver mBroadcastReceiver2 = new BroadcastReceiver()
@Override
public void onReceive(Context context, Intent intent)
final String action = intent.getAction();
Log.d(TAG, "onReceive: ACTION FOUND");
if(action.equals(BluetoothDevice.ACTION_FOUND))
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
// Yang dimasukin ke arraylist cuma BluetoothClass 1f00
if(device.getBluetoothClass().toString().equals("1f00"))
listDevice.add(device);
Log.d(TAG, "onReceive: " + device.getName() + ", " + device.getAddress());
rv_list_device.setHasFixedSize(true);
rv_list_device.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
BluetoothListAdapter listAdapter = new BluetoothListAdapter(listDevice,
HomeActivity.this, mBluetoothAdapter);
rv_list_device.setAdapter(listAdapter);
;
private BroadcastReceiver mBroadcastReceiver3 = new BroadcastReceiver()
@Override
public void onReceive(Context context, Intent intent)
final String action = intent.getAction();
if(action.equals(BluetoothDevice.ACTION_BOND_STATE_CHANGED))
BluetoothDevice mDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
if(mDevice.getBondState() == BluetoothDevice.BOND_BONDED)
Log.d(TAG, "BroadcastReceiver: BOND BONDED");
else if(mDevice.getBondState() == BluetoothDevice.BOND_BONDING)
Log.d(TAG, "BroadcastReceiver: BOND BONDING");
else if(mDevice.getBondState() == BluetoothDevice.BOND_NONE)
Log.d(TAG, "BroadcastReceiver: BOND NONE");
;
@BindView(R.id.sw_bluetooth) SwitchCompat sw_bluetooth;
@BindView(R.id.rv_list_device) RecyclerView rv_list_device;
@BindView(R.id.srl_list_parent) SwipeRefreshLayout srl_list_parent;
@BindView(R.id.tv_btWarning) TextView tv_btWarning;
private BluetoothListAdapter rvAdapter;
private ArrayList<BluetoothDevice> listDevice;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
ButterKnife.bind(this);
rv_list_device.setHasFixedSize(true);
rv_list_device.setLayoutManager(new LinearLayoutManager(this));
rvAdapter = new BluetoothListAdapter(listDevice, HomeActivity.this, mBluetoothAdapter);
rv_list_device.setAdapter(rvAdapter);
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
registerReceiver(mBroadcastReceiver3, filter);
listDevice = new ArrayList<>();
srl_list_parent.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener()
@Override
public void onRefresh()
refreshDevice();
new Handler().postDelayed(new Runnable()
@Override
public void run()
srl_list_parent.setRefreshing(false);
mBluetoothAdapter.cancelDiscovery();
Log.d(TAG, "onRefresh: Canceling discovery");
, 20000);
);
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
initialSwitch();
private void initialSwitch()
if(mBluetoothAdapter.isEnabled())
sw_bluetooth.setChecked(true);
srl_list_parent.setVisibility(View.VISIBLE);
tv_btWarning.setVisibility(View.GONE);
else if(!mBluetoothAdapter.isEnabled())
sw_bluetooth.setChecked(false);
srl_list_parent.setVisibility(View.GONE);
tv_btWarning.setVisibility(View.VISIBLE);
public void refreshDevice()
listDevice.clear();
Log.d(TAG, "refreshDevice: Looking for unpaired devices");
if(mBluetoothAdapter.isDiscovering())
mBluetoothAdapter.cancelDiscovery();
Log.d(TAG, "refreshDevice: Canceling discovery");
mBluetoothAdapter.startDiscovery();
IntentFilter discoverDevicesIntent = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(mBroadcastReceiver2, discoverDevicesIntent);
else if(!mBluetoothAdapter.isDiscovering())
mBluetoothAdapter.startDiscovery();
IntentFilter discoverDevicesIntent = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(mBroadcastReceiver2, discoverDevicesIntent);
@Override
protected void onDestroy()
Log.d(TAG, "onDestroy: called");
super.onDestroy();
try
if(mBroadcastReceiver1 != null)
unregisterReceiver(mBroadcastReceiver1);
mBroadcastReceiver1 = null;
catch(RuntimeException e)
Log.e(TAG, e.getMessage());
try
if(mBroadcastReceiver2 != null)
unregisterReceiver(mBroadcastReceiver2);
mBroadcastReceiver2 = null;
catch(RuntimeException e)
Log.e(TAG, e.getMessage());
try
if(mBroadcastReceiver3 != null)
unregisterReceiver(mBroadcastReceiver3);
mBroadcastReceiver3 = null;
catch(RuntimeException e)
Log.e(TAG, e.getMessage());
finish();
@OnCheckedChanged(R.id.sw_bluetooth)
public void onSwitch()
Log.d(TAG, "onSwitch: enabling/disabling bluetooth");
if(sw_bluetooth.isChecked() == true)
Log.d(TAG, "enableDisableBluetooth: enabling bluetooth");
srl_list_parent.setVisibility(View.VISIBLE);
tv_btWarning.setVisibility(View.GONE);
mBluetoothAdapter.enable();
IntentFilter BTIntent = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
registerReceiver(mBroadcastReceiver1, BTIntent);
else if(sw_bluetooth.isChecked() == false)
Log.d(TAG, "enableDisableBluetooth: disabling bluetooth");
mBluetoothAdapter.disable();
srl_list_parent.setVisibility(View.GONE);
tv_btWarning.setVisibility(View.VISIBLE);
listDevice.clear();
IntentFilter BTIntent = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
registerReceiver(mBroadcastReceiver1, BTIntent);
else if(mBluetoothAdapter == null)
Log.d(TAG, "enableDisableBluetooth: Doesn't have BT capabilities");
I could find my RFID reader device (it shows on the list), but I couldn't pair it. I've searched on how to do this but can't seem to find how to do it.
android android-bluetooth
I've been trying to develop an app which I could integrate with a RFID reader (I'm using a 1128 UHF RFID Reader). I've recently trying to learn how to use Bluetooth in android studio, i have create a simple app where I could turn on Bluetooth, find devices and pair with them. But I couldn't pair with the RFID reader. I'm new to android programming so I'm not sure how to do it properly.
Here is my code :
package com.siscaproject.sisca.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.SwitchCompat;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import com.siscaproject.sisca.Adapter.BluetoothListAdapter;
import com.siscaproject.sisca.R;
import java.util.ArrayList;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnCheckedChanged;
public class HomeActivity extends AppCompatActivity
private static final String TAG = "HomeActivity";
private BluetoothAdapter mBluetoothAdapter;
private BroadcastReceiver mBroadcastReceiver1 = new BroadcastReceiver()
@Override
public void onReceive(Context context, Intent intent)
String action = intent.getAction();
if(action.equals(mBluetoothAdapter.ACTION_STATE_CHANGED))
final int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, mBluetoothAdapter.ERROR);
switch(state)
case BluetoothAdapter.STATE_OFF:
Log.d(TAG, "onReceive: STATE OFF");
break;
case BluetoothAdapter.STATE_TURNING_OFF:
Log.d(TAG, "onReceive: STATE TURNING OFF");
break;
case BluetoothAdapter.STATE_ON:
Log.d(TAG, "onReceive: STATE ON");
break;
case BluetoothAdapter.STATE_TURNING_ON:
Log.d(TAG, "onReceive: STATE TURNING ON");
break;
;
private BroadcastReceiver mBroadcastReceiver2 = new BroadcastReceiver()
@Override
public void onReceive(Context context, Intent intent)
final String action = intent.getAction();
Log.d(TAG, "onReceive: ACTION FOUND");
if(action.equals(BluetoothDevice.ACTION_FOUND))
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
// Yang dimasukin ke arraylist cuma BluetoothClass 1f00
if(device.getBluetoothClass().toString().equals("1f00"))
listDevice.add(device);
Log.d(TAG, "onReceive: " + device.getName() + ", " + device.getAddress());
rv_list_device.setHasFixedSize(true);
rv_list_device.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
BluetoothListAdapter listAdapter = new BluetoothListAdapter(listDevice,
HomeActivity.this, mBluetoothAdapter);
rv_list_device.setAdapter(listAdapter);
;
private BroadcastReceiver mBroadcastReceiver3 = new BroadcastReceiver()
@Override
public void onReceive(Context context, Intent intent)
final String action = intent.getAction();
if(action.equals(BluetoothDevice.ACTION_BOND_STATE_CHANGED))
BluetoothDevice mDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
if(mDevice.getBondState() == BluetoothDevice.BOND_BONDED)
Log.d(TAG, "BroadcastReceiver: BOND BONDED");
else if(mDevice.getBondState() == BluetoothDevice.BOND_BONDING)
Log.d(TAG, "BroadcastReceiver: BOND BONDING");
else if(mDevice.getBondState() == BluetoothDevice.BOND_NONE)
Log.d(TAG, "BroadcastReceiver: BOND NONE");
;
@BindView(R.id.sw_bluetooth) SwitchCompat sw_bluetooth;
@BindView(R.id.rv_list_device) RecyclerView rv_list_device;
@BindView(R.id.srl_list_parent) SwipeRefreshLayout srl_list_parent;
@BindView(R.id.tv_btWarning) TextView tv_btWarning;
private BluetoothListAdapter rvAdapter;
private ArrayList<BluetoothDevice> listDevice;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
ButterKnife.bind(this);
rv_list_device.setHasFixedSize(true);
rv_list_device.setLayoutManager(new LinearLayoutManager(this));
rvAdapter = new BluetoothListAdapter(listDevice, HomeActivity.this, mBluetoothAdapter);
rv_list_device.setAdapter(rvAdapter);
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
registerReceiver(mBroadcastReceiver3, filter);
listDevice = new ArrayList<>();
srl_list_parent.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener()
@Override
public void onRefresh()
refreshDevice();
new Handler().postDelayed(new Runnable()
@Override
public void run()
srl_list_parent.setRefreshing(false);
mBluetoothAdapter.cancelDiscovery();
Log.d(TAG, "onRefresh: Canceling discovery");
, 20000);
);
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
initialSwitch();
private void initialSwitch()
if(mBluetoothAdapter.isEnabled())
sw_bluetooth.setChecked(true);
srl_list_parent.setVisibility(View.VISIBLE);
tv_btWarning.setVisibility(View.GONE);
else if(!mBluetoothAdapter.isEnabled())
sw_bluetooth.setChecked(false);
srl_list_parent.setVisibility(View.GONE);
tv_btWarning.setVisibility(View.VISIBLE);
public void refreshDevice()
listDevice.clear();
Log.d(TAG, "refreshDevice: Looking for unpaired devices");
if(mBluetoothAdapter.isDiscovering())
mBluetoothAdapter.cancelDiscovery();
Log.d(TAG, "refreshDevice: Canceling discovery");
mBluetoothAdapter.startDiscovery();
IntentFilter discoverDevicesIntent = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(mBroadcastReceiver2, discoverDevicesIntent);
else if(!mBluetoothAdapter.isDiscovering())
mBluetoothAdapter.startDiscovery();
IntentFilter discoverDevicesIntent = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(mBroadcastReceiver2, discoverDevicesIntent);
@Override
protected void onDestroy()
Log.d(TAG, "onDestroy: called");
super.onDestroy();
try
if(mBroadcastReceiver1 != null)
unregisterReceiver(mBroadcastReceiver1);
mBroadcastReceiver1 = null;
catch(RuntimeException e)
Log.e(TAG, e.getMessage());
try
if(mBroadcastReceiver2 != null)
unregisterReceiver(mBroadcastReceiver2);
mBroadcastReceiver2 = null;
catch(RuntimeException e)
Log.e(TAG, e.getMessage());
try
if(mBroadcastReceiver3 != null)
unregisterReceiver(mBroadcastReceiver3);
mBroadcastReceiver3 = null;
catch(RuntimeException e)
Log.e(TAG, e.getMessage());
finish();
@OnCheckedChanged(R.id.sw_bluetooth)
public void onSwitch()
Log.d(TAG, "onSwitch: enabling/disabling bluetooth");
if(sw_bluetooth.isChecked() == true)
Log.d(TAG, "enableDisableBluetooth: enabling bluetooth");
srl_list_parent.setVisibility(View.VISIBLE);
tv_btWarning.setVisibility(View.GONE);
mBluetoothAdapter.enable();
IntentFilter BTIntent = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
registerReceiver(mBroadcastReceiver1, BTIntent);
else if(sw_bluetooth.isChecked() == false)
Log.d(TAG, "enableDisableBluetooth: disabling bluetooth");
mBluetoothAdapter.disable();
srl_list_parent.setVisibility(View.GONE);
tv_btWarning.setVisibility(View.VISIBLE);
listDevice.clear();
IntentFilter BTIntent = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
registerReceiver(mBroadcastReceiver1, BTIntent);
else if(mBluetoothAdapter == null)
Log.d(TAG, "enableDisableBluetooth: Doesn't have BT capabilities");
I could find my RFID reader device (it shows on the list), but I couldn't pair it. I've searched on how to do this but can't seem to find how to do it.
android android-bluetooth
android android-bluetooth
edited Nov 12 '18 at 10:43
Gayan Mettananda
924612
924612
asked Nov 12 '18 at 8:42
llehcramllehcram
256
256
add a comment |
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53258469%2fconnect-to-a-rfid-reader-using-android-bluetooth%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53258469%2fconnect-to-a-rfid-reader-using-android-bluetooth%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown