I'm working on a assigment for school and need to send a bluetooth signal to an HC-05 device. This is what I've so far but it keeps giving the same error message.
D/BluetoothUtils: isSocketAllowedBySecurityPolicy start : device null W/BluetoothAdapter: getBluetoothService() called with no BluetoothManagerCallback E/BluetoothAdapter: notifyPackageNameForWifi com.example.opdracht9/10220
This is my code:
package com.example.opdracht9;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import android.Manifest;
import android.annotation.SuppressLint;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothManager;
import android.bluetooth.BluetoothSocket;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.UUID;
@RequiresApi(api = Build.VERSION_CODES.M)
public class MainActivity extends AppCompatActivity {
static final UUID mUUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
@SuppressLint("MissingPermission")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (ContextCompat.checkSelfPermission(MainActivity.this,
Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED){
if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this,
Manifest.permission.ACCESS_FINE_LOCATION)){
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
}else{
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
}
}
// Use this check to determine whether Bluetooth classic is supported on the device.
// Then you can selectively disable BLE-related features.
if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH)) {
Toast.makeText(this, "R.string.bluetooth_not_supported", Toast.LENGTH_SHORT).show();
finish();
}
// Use this check to determine whether BLE is supported on the device. Then
// you can selectively disable BLE-related features.
if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
Toast.makeText(this, "R.string.ble_not_supported", Toast.LENGTH_SHORT).show();
finish();
}
BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();
System.out.println(btAdapter.getBondedDevices());
BluetoothDevice hc05 = btAdapter.getRemoteDevice("98:D3:61:FD:35:1E");
System.out.println(hc05.getName());
BluetoothSocket btSocket = null;
int counter = 0;
do {
try {
btSocket = hc05.createRfcommSocketToServiceRecord(mUUID);
System.out.println(btSocket);
btSocket.connect();
System.out.println("Connection is: ");
System.out.println(btSocket.isConnected());
} catch (IOException e) {
e.printStackTrace();
}
counter++;
}while( !btSocket.isConnected() && counter<3);
try {
OutputStream outputStream = btSocket.getOutputStream();
outputStream.write(0);
} catch (IOException e) {
e.printStackTrace();
}
try {
btSocket.close();
System.out.println(btSocket.isConnected());
} catch (IOException e) {
e.printStackTrace();
}
}
public void Stuur(View v){
}
@SuppressLint("MissingSuperCall")
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions,
int[] grantResults){
switch (requestCode){
case 1: {
if (grantResults.length>0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
if (ContextCompat.checkSelfPermission(MainActivity.this,
Manifest.permission.ACCESS_FINE_LOCATION)==PackageManager.PERMISSION_GRANTED){
Toast.makeText(this, "Permission Granted", Toast.LENGTH_SHORT).show();
}
}else{
Toast.makeText(this, "Permission Denied", Toast.LENGTH_SHORT).show();
}
return;
}
}
}
}
And this is my Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.opdracht9">
<uses-permission android:name="android.permission.BLUETOOTH" android:required="true"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" android:required="true"/>
<uses-feature android:name="android.hardware.bluetooth" android:required="true"/>
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" android:required="true"/>
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION"
android:required="true"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Opdracht9"
tools:targetApi="31">
<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>
</manifest>
I've no idea why it does not work, I've access to the bluetooth of the device I'm working on and the device is connected to the HC-05.