Skip to content

Changes to sample to work with Android 7+ devices #18

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.2'
classpath 'com.android.tools.build:gradle:2.3.3'
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.4.1'
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.0'

Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip
6 changes: 3 additions & 3 deletions lmbluetoothsdk/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ apply plugin: 'com.android.library'

android {
compileSdkVersion 25
buildToolsVersion "25.0.0"
buildToolsVersion "25.0.3"

defaultConfig {
minSdkVersion 9
targetSdkVersion 23
targetSdkVersion 25
versionCode 105
versionName "1.0.5"
}
Expand All @@ -20,4 +20,4 @@ android {

dependencies {
}
apply from: 'https://raw.githubusercontent.com/whilu/AndroidPublishLibrary/master/project/library/bintray_publish_sign.gradle'

12 changes: 6 additions & 6 deletions sample/build.gradle
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 23
buildToolsVersion "23.0.3"
compileSdkVersion 25
buildToolsVersion "25.0.3"

defaultConfig {
applicationId "co.lujun.sample"
minSdkVersion 9
targetSdkVersion 23
minSdkVersion 21
targetSdkVersion 25
versionCode 1
versionName "1.0"
}
Expand All @@ -23,6 +23,6 @@ dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile project(':lmbluetoothsdk')
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:design:23.1.1'
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support:design:25.3.1'
}
11 changes: 9 additions & 2 deletions sample/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

<application
android:allowBackup="true"
Expand All @@ -14,7 +18,9 @@
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
android:theme="@style/AppTheme.NoActionBar"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

Expand All @@ -24,7 +30,8 @@
<activity android:name=".ClassicBluetoothActivity" />
<activity
android:name=".ChatActivity"
android:windowSoftInputMode="adjustPan" />
android:windowSoftInputMode="adjustPan"
android:exported="true"/>
<activity
android:name=".BleActivity"
android:windowSoftInputMode="adjustPan" />
Expand Down
142 changes: 135 additions & 7 deletions sample/src/main/java/co/lujun/sample/ChatActivity.java
Original file line number Diff line number Diff line change
@@ -1,24 +1,32 @@
package co.lujun.sample;

import android.app.Activity;
import android.bluetooth.BluetoothA2dp;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothHeadset;
import android.bluetooth.BluetoothManager;
import android.bluetooth.BluetoothProfile;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.lang.reflect.Method;

import co.lujun.lmbluetoothsdk.BluetoothController;
import co.lujun.lmbluetoothsdk.base.BluetoothListener;
import co.lujun.lmbluetoothsdk.base.State;

/**
* Author: lujun(http://blog.lujun.co)
* Date: 2016-1-21 16:10
*/
public class ChatActivity extends Activity {
public class ChatActivity extends Activity{
private BluetoothController mBluetoothController;

private Button btnDisconnect, btnSend;
Expand All @@ -29,12 +37,37 @@ public class ChatActivity extends Activity {
private String mMacAddress = "", mDeviceName = "";

private static final String TAG = "LMBluetoothSdk";
private BluetoothHeadset bluetoothHeadset;
private boolean isServiceConnected = false;
private boolean isHeadsetConnected = false;

private BluetoothDevice mDevice;
private BluetoothManager mBluetoothManager;
private BluetoothAdapter mBluetoothAdapter;
public BluetoothProfile headset;
public BluetoothProfile a2Dp;


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.chat_main);

if (mBluetoothManager == null) {
mBluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
if (mBluetoothManager == null) {
Log.e(TAG, "Unable to initialize BluetoothManager.");
return ;
}
}

mBluetoothAdapter = mBluetoothManager.getAdapter();
mBluetoothAdapter.getProfileProxy(getApplicationContext(), mServiceListener, BluetoothProfile.HEADSET);
mBluetoothAdapter.getProfileProxy(getApplicationContext(), mA2dpServiceListener, BluetoothProfile.A2DP);
if (mBluetoothAdapter == null) {
Log.e(TAG, "Unable to obtain a BluetoothAdapter.");
return;
}

init();
}

Expand Down Expand Up @@ -111,15 +144,37 @@ public void onClick(View v) {
btnDisconnect.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mConnectState == State.STATE_CONNECTED) {
mBluetoothController.disconnect();
// if (mConnectState == State.STATE_CONNECTED) {
// mBluetoothController.disconnect();
//// unpair(mDevice);
// }
Method connect = null;
Method setPriority = null;
Method disconnectA2dp = null;
Method seta2dppriority = null;
try {
setPriority = BluetoothHeadset.class.getDeclaredMethod("setPriority", BluetoothDevice.class, int.class);
connect = BluetoothHeadset.class.getDeclaredMethod("disconnect", BluetoothDevice.class);
seta2dppriority = BluetoothA2dp.class.getDeclaredMethod("setPriority", BluetoothDevice.class, int.class);
disconnectA2dp = BluetoothA2dp.class.getDeclaredMethod("disconnect", BluetoothDevice.class);
setPriority.setAccessible(true);
setPriority.invoke(headset, mDevice, 100);
connect.setAccessible(true);
connect.invoke(headset, mDevice);
seta2dppriority.setAccessible(true);
seta2dppriority.invoke(a2Dp, mDevice, 100);
disconnectA2dp.setAccessible(true);
disconnectA2dp.invoke(a2Dp, mDevice);
} catch (Throwable e) {
e.printStackTrace();
}
finish();
// finish();
}
});

if (!TextUtils.isEmpty(mMacAddress)) {
mBluetoothController.connect(mMacAddress);
// mBluetoothController.connect(mMacAddress);
mDevice = mBluetoothAdapter.getRemoteDevice(mMacAddress);
}else {
if (mBluetoothController.getConnectedDevice() == null){
return;
Expand All @@ -130,4 +185,77 @@ public void onClick(View v) {
tvDeviceMac.setText("MAC address: " + mMacAddress);
}
}


// @Override
// public void onServiceConnected() {
// isServiceConnected = true;
// if(!isHeadsetConnected && mDevice != null){
// if(bluetoothHeadset.connectHeadset(mDevice)){
// isHeadsetConnected = true;
// }
// }
// }
//
// @Override
// public void onServiceDisconnected() {
// isServiceConnected = false;
// }

private BluetoothProfile.ServiceListener mServiceListener = new BluetoothProfile.ServiceListener() {

@Override
public void onServiceConnected(int profile, BluetoothProfile proxy) {
if (profile == BluetoothProfile.HEADSET) {
Log.i(TAG, "Headset proxy connected");
headset = proxy;
try {
Method connect = BluetoothHeadset.class.getDeclaredMethod("connect", BluetoothDevice.class);
connect.setAccessible(true);
connect.invoke(proxy, mDevice);

} catch (Throwable e) {
e.printStackTrace();
}
}
}

@Override
public void onServiceDisconnected(int profile) {
headset = null;
Log.i(TAG, "Headset disconnected");
}
};

private BluetoothProfile.ServiceListener mA2dpServiceListener = new BluetoothProfile.ServiceListener() {
@Override
public void onServiceConnected(int profile, BluetoothProfile proxy) {
if (profile == BluetoothProfile.A2DP) {
a2Dp = proxy;
}
}

@Override
public void onServiceDisconnected(int profile) {
a2Dp = null;
}
};

private static boolean unpair(BluetoothDevice device) {
Method sRmBd = null;
try {
sRmBd =BluetoothDevice.class.getMethod("removeBond", (Class[]) null);
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
if (sRmBd != null) {
try {
sRmBd.invoke(device, (Object[]) null);
return true;
} catch (Throwable t) {
Log.e(TAG , ":" + "unpair: error", t);
}
}
return false;
}
}