public void startScan() {
final List<MyBluetoothDevice> arrayOfFoundBTDevices = new ArrayList<>();
// start looking for bluetooth devices
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
boolean scanStatus = mBluetoothAdapter.startDiscovery();
Timber.d("SCANNING_STATUS : " + scanStatus);
// Discover new devices
// Create a BroadcastReceiver for ACTION_FOUND
mReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
Timber.d("onReceiveSignal");
String action = intent.getAction();
// When discovery finds a device
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
// Get the bluetoothDevice object from the Intent
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
} else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
Timber.d("Discovery Finished ");
AppUtils.showToast(context, "Scanning restart");
mBluetoothAdapter.startDiscovery();
}
}
};
IntentFilter filter = new IntentFilter();
filter.addAction(BluetoothDevice.ACTION_FOUND);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
context.registerReceiver(mReceiver, filter);
}
I am try to scan nearby bluetooth devices.
But, BluetoothAdapter.getDefaultAdapter().startDiscovery() method returns false in API Level 29 but working in API Level 26
-------------Permission defined in AndroidManifest.xml-----------
Can't find solution for Android 10
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.bluetoothexample">
<uses-feature
android:name="android.hardware.bluetooth"
android:required="true" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<application
android:name=".App"
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".FoundBTDevices" />
</application>
</manifest>
You need to turn ON location to scan nearby devices in Android 10.
not sure if you are still looking for the answer.
From API 29, Google requires LOCATION group permission to use some Bluetooth function. So you will need to add some <uses-permission> tag to Android Manifest, and DON'T forget to request these permissions at RUNTIME.
I don't remember exactly which location is needed, so you can add all 3 following:
android.permission.ACCESS_FINE_LOCATION
android.permission.ACCESS_COARSE_LOCATION
android.permission.ACCESS_BACKGROUND_LOCATION
Related
I'm trying to do a quick test app that allow me to turn on a off Bluetooth and I code the listeners for clicks in the buttons. This is the one that turn Bluetooth on:
mOnBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (!mBlueAdapter.isEnabled()) {
showToast("Turning On Bluetooth...");
//intent to on BT
Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
//Android Studio force me to do this check.
if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {
showToast("I'm stuck here");
return;
}
startActivityForResult(intent, REQUEST_ENABLE_BT);
} else {
showToast("Bluetooth is already on");
}
}
});
The thing is that Android Studio force me to add the:
if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {
showToast("I'm stuck here");
return;
}
check error but even I have all the permissions they asked me in AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.BLUETOOTH_ADVERTISE" />
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
<uses-permission android:name="android.permission.BLUETOOTH_SCAN" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/Theme.Bluetooth">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
the program always fails the check error and ended up in the "You're stuck message", what is happening here?
Turning bluetooth on and requesting `BLUETOOTH_CONNECT permission is a different thing.
Request permission like BLUETOOTH_CONNECT, see https://developer.android.com/training/permissions/requesting.
Turning bluetooth on
if (bluetoothAdapter?.isEnabled == false) {
val enableBtIntent = Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE)
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT)
}
If enabling Bluetooth succeeds, your activity receives the RESULT_OK result code in the onActivityResult() callback. If Bluetooth was not enabled due to an error (or the user responded "Deny") then the result code is RESULT_CANCELED.
See Set up Bluetooth for details.
The permissionsList.add() doesn't work but MainActivity.this.requestPermissions() works fine. The issue is that it brings a dialog box asking if the user allow the location permission.
Why adding the permission does not work?
Is there a way to avoid the dialog box?
See my minimal code below:
public class MainActivity extends AppCompatActivity {
WifiManager wifiManager;
WifiBroadcastReceiver broadcastReceiver;
Context context;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = getApplicationContext();
List<String> permissionsList = new ArrayList<String>();
permissionsList.add(Manifest.permission.ACCESS_FINE_LOCATION);
permissionsList.add(Manifest.permission.ACCESS_COARSE_LOCATION);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if(checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
MainActivity.this.requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 100);
MainActivity.this.requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, 100);
}
}
Button scan = (Button) findViewById(R.id.scan);
wifiManager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
wifiManager.setWifiEnabled(true);
scan.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if(wifiManager != null)
wifiManager.startScan();
}
});
broadcastReceiver = new WifiBroadcastReceiver();
// On attache le receiver au scan result
registerReceiver(broadcastReceiver, new IntentFilter(
WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
}
public class WifiBroadcastReceiver extends BroadcastReceiver {
private WifiManager wifiManager;
#Override
public void onReceive(Context context, Intent intent) {
wifiManager = ((MainActivity) context).getCurrentWifiManager();
List<ScanResult> listeScan = wifiManager.getScanResults();
}
}
public WifiManager getCurrentWifiManager() {
return wifiManager;
}
}
And here is the manifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.bernard_zelmans.checksecurity">
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Ping"></activity>
</application>
</manifest>
Beginning in Android 6.0 (API level 23), users grant permissions to
apps while the app is running, not when they install the app. This
approach streamlines the app install process, since the user does not
need to grant permissions when they install or update the app. It also
gives the user more control over the app's functionality; for example,
a user could choose to give a camera app access to the camera but not
to the device location. The user can revoke the permissions at any
time, by going to the app's Settings screen.
Resource and more to read:
https://developer.android.com/training/permissions/requesting.html
https://developer.android.com/guide/topics/security/permissions.html#normal-dangerous
I want to get call details(mobile number, call duration, date, time etc) of dialed mobile no after disconnecting call.
What I did so far:
I create a Broadcast Receiver to detect the call disconnect event. After getting call details I fetch the latest dialed no from call log and store in SQLite database.
What the problem is:
When I dial any no from device and disconnect that, onReceive() method called twice. Same record is inserted twice. I have checked it by printing Logs also.
I searched this issue on Google and got some solution like " use sendBroadcast() only once, register broadcast receiver only once etc". But I am not calling sendBroadcast()anywhere, neither I am registering it twice.
I am new to Android so please suggest what am I doing wrong?
Broadcast Receiver:
public class CallReceiver extends BroadcastReceiver {
ContentResolver contentResolver;
Context context;
boolean is_network_roaming = false;
TelephonyManager tm = null;
AppInfo appInfo = null;
ContactHelper contactHelper;
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
String TAG = getClass().getSimpleName();
this.context = context;
tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
appInfo = new AppInfo(context);
contactHelper = new ContactHelper(context);
if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) {
is_network_roaming = tm.isNetworkRoaming();
/* Method for getting call details from call log */
getAllCallLogs();
}
}
Manifest File:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.callduration"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="22" />
<uses-permission android:name="android.permission.READ_CALL_LOG" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_logo"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.callduration.Splash"
android:configChanges="orientation|keyboardHidden"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:theme="#android:style/Theme.NoTitleBar"
android:windowSoftInputMode="adjustPan|adjustResize|stateHidden" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.callduration.MainActivity" >
</activity>
<activity android:name="com.callduration.CallHistory" >
</activity>
<receiver android:name="com.callduration.receivers.CallReceiver" >
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
</application>
</manifest>
The sequence of state changes when dialing a call is:
CALL_STATE_OFFHOOK--->ACTION_NEW_OUTGOING_CALL-->...{call answered or not
answered}.........--> CALL_STATE_OFFHOOK-->CALL_STATE_IDLE
As per the above states, your onReceive is supposed to start twice, once when call is initiated and second when call is disconnected. To achieve what you need i.e you want to log the events of a single call session. You can use the below code,
int state=intent.getStringExtra(TelephonyManager.EXTRA_STATE)
Boolean singlecallstate=false;
switch (state) {
case TelephonyManager.ACTION_NEW_OUTGOING_CALL:
singlecallstate=true;
//any other code you want
case TelephonyManager.CALL_STATE_IDLE:
if(singlecallstate){
getAllCallLogs();
singlecallstate=false;
}
I am new to android programming, my main aim is to communicate over USB to an MCU using FT200XD USB to I2C bridge.
First I am trying to detect attached USB device via the UsbManager. From what I understand, at on create a popup window should ask permission to connect from the user but no permission is asked. While debugging its clear that the control doesn't go into the broadcast receiver section.
I have refereed few example code snippet and wrote the code below. I don't know what I am doing wrong.
I have downloaded an app called"USB host Controller" which does detect the FT200XD. Which means my tablet has the USB host functionality. It will be great if you can point me to the right direction or an entire working code can be shared.
My code is as follows:
Java file:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fullscreen);
mUsbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
mPermissionIntent= PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);
IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
filter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED);
filter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED);
filter.addAction(UsbManager.EXTRA_PERMISSION_GRANTED);
filter.addAction(ACTION_USB_PERMISSION);
registerReceiver(mUsbReceiver, filter);
}
// Broadcast receiver
public class mUsbReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
Toast.makeText(getApplicationContext(),
"Inside USB Broadcast", Toast.LENGTH_SHORT).show();
}
}
Manifest file part:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.usb"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="12"
android:targetSdkVersion="19" />
<uses-feature android:name="android.hardware.usb.host" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<receiver android:name="mUsbReceiver">
<intent-filter>
<action android:name="android.hardware.usb.action.ACTION_USB_PERMISSION"/>
</intent-filter>
</receiver>
<activity
android:name="com.example.usb.FullscreenActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="#string/app_name"
android:theme="#style/FullscreenTheme" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.hardware.usb.action.USB_DEVICE_DETACHED" />
<action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
</intent-filter>
<meta-data android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
android:resource="#xml/device_filter" />
</activity>
</application>
</manifest>
Device_filter.xml file:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- 0x0403 / 0x6001: FTDI FT232R UART -->
<usb-device vendor-id="1027" product-id="24577" />
<!-- 0x2341 / Arduino -->
<usb-device vendor-id="9025" />
<!-- 0x16C0 / 0x0483: Teensyduino -->
<usb-device vendor-id="5824" product-id="1155" />
<!-- 0x617 / 0x000b: EFPL CC2531 -->
<usb-device vendor-id="1559" product-id="11" />
<!-- vendor-id="0x0403" product-id="0x6015" // converted to Int vendor ID and product ID of my FT200XD-->
<usb-device vendor-id="1027" product-id="24597" />
</resources>
You probably want to look at the return code for registerReceiver - from what you are posting in your code I'd assume it would be failing since you don't actually instantiate your mUsbReceiver. Take a look at this code, which is from extracted from an application I've written that works, notice the difference in the way I'm setting up my BroadcastReceiver, this also shows how to request permission if your device is inserted:
PendingIntent mPermissionIntent = null;
UsbManager mUsbManager = null;
private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
UsbDevice usbDevice = (UsbDevice)intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
if (ACTION_USB_PERMISSION.equals(action)) {
// Permission requested
synchronized (this) {
if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
// User has granted permission
// ... Setup your UsbDeviceConnection via mUsbManager.openDevice(usbDevice) ...
} else {
// User has denied permission
}
}
}
if (UsbManager.ACTION_USB_DEVICE_DETACHED.equals(action)) {
// Device removed
synchronized (this) {
// ... Check to see if usbDevice is yours and cleanup ...
}
}
if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(action)) {
// Device attached
synchronized (this) {
// Qualify the new device to suit your needs and request permission
if ((usbDevice.getVendorId() == MY_VID) && (usbDevice.getProductId() == MY_PID)) {
mUsbManager.requestPermission(usbDevice, mPermissionIntent);
}
}
}
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
// ... App Specific Setup Here ...
mUsbManager = (UsbManager)getSystemService(Context.USB_SERVICE);
// Register an intent filter so we can get permission to connect
// to the device and get device attached/removed messages
mPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);
IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
filter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED);
filter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED);
registerReceiver(mUsbReceiver, filter);
// ... More App Specific Setup ...
}
Also - it appears that for my app I did not need the extra intent-filter or meta-data XML for the USB actions.
I have USB host android device for that I need to connect USB device. to detect usb device to host I written following code.
public class ReadData extends Activity {
UsbManager usbManager;
PendingIntent mPermissionIntent;
UsbDevice usbDevice;
Intent intent;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_read_data);
usbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
final String ACTION_USB_PERMISSION =
"com.example.udevice.USB_PERMISSION";
IntentFilter filter = new IntentFilter("android.hardware.usb.action.USB_ACCESSORY_ATTACHED");
registerReceiver(mUsbReceiver, filter);
}
private static final String ACTION_USB_PERMISSION =
"com.example.udevice.USB_PERMISSION";
private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (ACTION_USB_PERMISSION.equals(action)) {
synchronized (this) {
usbDevice = (UsbDevice) intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
usbManager.requestPermission(usbDevice, mPermissionIntent);
if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
if(usbDevice != null){
//call method to set up device communication
int deviceId = usbDevice.getDeviceId();
int productId = usbDevice.getProductId();
Log.i("device id", "****"+deviceId);
Log.i("product id", "****"+productId);
}else{
Log.i("device id", "No USB device");
}
}
else {
Log.d("shiv", "permission denied for device ");
}
}
}
}
};
and manifest is like below:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.udevice"
android:versionCode="1"
android:versionName="1.0" >
<uses-feature android:name="android.hardware.usb.host" />
<uses-sdk
android:minSdkVersion="12"
android:targetSdkVersion="15" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".ReadData"
android:label="#string/title_activity_heat_con" >
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
android:resource="#xml/device_filter" />
</activity>
</application>
</manifest>
device_filter.xml
<resources>
<usb-device vendor-id="67b"
product-id="2303"/>
</resources>
in above xml file I added device attributes. I am expecting a broadcast intent whenever USB device connected to host device. but it is not happening. What is wrong with above code.
Thanks
shiv
I think you need to add:
<intent-filter>
<action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
</intent-filter>
It is described here
There is onething you are doing it wrong.
The vendor id and device id should be in decimals not in hex. For example, you need to define as follows
<resources>
<usb-device vendor-id="1659"
product-id="8963"/>
</resources>
I converted your device id and vendor-id from hex to decimal
Let me know if this helps