mirror of
https://github.com/bluekitchen/btstack.git
synced 2025-01-18 19:21:54 +00:00
SPPClient android project
This commit is contained in:
parent
8e6a21a25f
commit
8756dc50de
11
platforms/mtk/SPPClient/.classpath
Normal file
11
platforms/mtk/SPPClient/.classpath
Normal file
@ -0,0 +1,11 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry kind="src" path="btstack-gen"/>
|
||||
<classpathentry kind="src" path="btstack-src"/>
|
||||
<classpathentry kind="con" path="com.android.ide.eclipse.adt.ANDROID_FRAMEWORK"/>
|
||||
<classpathentry exported="true" kind="con" path="com.android.ide.eclipse.adt.LIBRARIES"/>
|
||||
<classpathentry exported="true" kind="con" path="com.android.ide.eclipse.adt.DEPENDENCIES"/>
|
||||
<classpathentry kind="src" path="src"/>
|
||||
<classpathentry kind="src" path="gen"/>
|
||||
<classpathentry kind="output" path="bin/classes"/>
|
||||
</classpath>
|
51
platforms/mtk/SPPClient/.project
Normal file
51
platforms/mtk/SPPClient/.project
Normal file
@ -0,0 +1,51 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>SPPClient</name>
|
||||
<comment></comment>
|
||||
<projects>
|
||||
</projects>
|
||||
<buildSpec>
|
||||
<buildCommand>
|
||||
<name>com.android.ide.eclipse.adt.ResourceManagerBuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
<buildCommand>
|
||||
<name>com.android.ide.eclipse.adt.PreCompilerBuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
<buildCommand>
|
||||
<name>com.android.ide.eclipse.adt.ApkBuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>com.android.ide.eclipse.adt.AndroidNature</nature>
|
||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||
</natures>
|
||||
<linkedResources>
|
||||
<link>
|
||||
<name>btstack-gen</name>
|
||||
<type>2</type>
|
||||
<locationURI>BTSTACK_JAVA_LOC/gen</locationURI>
|
||||
</link>
|
||||
<link>
|
||||
<name>btstack-src</name>
|
||||
<type>2</type>
|
||||
<locationURI>BTSTACK_JAVA_LOC/src</locationURI>
|
||||
</link>
|
||||
</linkedResources>
|
||||
<variableList>
|
||||
<variable>
|
||||
<name>BTSTACK_JAVA_LOC</name>
|
||||
<value>file:/Projects/btstack-xcode/btstack/java</value>
|
||||
</variable>
|
||||
</variableList>
|
||||
</projectDescription>
|
@ -0,0 +1,4 @@
|
||||
eclipse.preferences.version=1
|
||||
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
|
||||
org.eclipse.jdt.core.compiler.compliance=1.6
|
||||
org.eclipse.jdt.core.compiler.source=1.6
|
27
platforms/mtk/SPPClient/AndroidManifest.xml
Normal file
27
platforms/mtk/SPPClient/AndroidManifest.xml
Normal file
@ -0,0 +1,27 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
package="com.bluekitchen.sppclient"
|
||||
android:versionCode="1"
|
||||
android:versionName="1.0" >
|
||||
|
||||
<uses-sdk
|
||||
android:minSdkVersion="8"
|
||||
android:targetSdkVersion="18" />
|
||||
|
||||
<application
|
||||
android:allowBackup="true"
|
||||
android:icon="@drawable/ic_launcher"
|
||||
android:label="@string/app_name"
|
||||
android:theme="@style/AppTheme" >
|
||||
<activity
|
||||
android:name="com.bluekitchen.sppclient.MainActivity"
|
||||
android:label="@string/app_name" >
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.MAIN" />
|
||||
|
||||
<category android:name="android.intent.category.LAUNCHER" />
|
||||
</intent-filter>
|
||||
</activity>
|
||||
</application>
|
||||
|
||||
</manifest>
|
27
platforms/mtk/SPPClient/bin/AndroidManifest.xml
Normal file
27
platforms/mtk/SPPClient/bin/AndroidManifest.xml
Normal file
@ -0,0 +1,27 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
package="com.bluekitchen.sppclient"
|
||||
android:versionCode="1"
|
||||
android:versionName="1.0" >
|
||||
|
||||
<uses-sdk
|
||||
android:minSdkVersion="8"
|
||||
android:targetSdkVersion="18" />
|
||||
|
||||
<application
|
||||
android:allowBackup="true"
|
||||
android:icon="@drawable/ic_launcher"
|
||||
android:label="@string/app_name"
|
||||
android:theme="@style/AppTheme" >
|
||||
<activity
|
||||
android:name="com.bluekitchen.sppclient.MainActivity"
|
||||
android:label="@string/app_name" >
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.MAIN" />
|
||||
|
||||
<category android:name="android.intent.category.LAUNCHER" />
|
||||
</intent-filter>
|
||||
</activity>
|
||||
</application>
|
||||
|
||||
</manifest>
|
@ -0,0 +1,6 @@
|
||||
/** Automatically generated file. DO NOT MODIFY */
|
||||
package com.bluekitchen.sppclient;
|
||||
|
||||
public final class BuildConfig {
|
||||
public final static boolean DEBUG = true;
|
||||
}
|
68
platforms/mtk/SPPClient/gen/com/bluekitchen/sppclient/R.java
Normal file
68
platforms/mtk/SPPClient/gen/com/bluekitchen/sppclient/R.java
Normal file
@ -0,0 +1,68 @@
|
||||
/* AUTO-GENERATED FILE. DO NOT MODIFY.
|
||||
*
|
||||
* This class was automatically generated by the
|
||||
* aapt tool from the resource data it found. It
|
||||
* should not be modified by hand.
|
||||
*/
|
||||
|
||||
package com.bluekitchen.sppclient;
|
||||
|
||||
public final class R {
|
||||
public static final class attr {
|
||||
}
|
||||
public static final class dimen {
|
||||
/** Default screen margins, per the Android Design guidelines.
|
||||
|
||||
Customize dimensions originally defined in res/values/dimens.xml (such as
|
||||
screen margins) for sw720dp devices (e.g. 10" tablets) in landscape here.
|
||||
|
||||
*/
|
||||
public static final int activity_horizontal_margin=0x7f040000;
|
||||
public static final int activity_vertical_margin=0x7f040001;
|
||||
}
|
||||
public static final class drawable {
|
||||
public static final int ic_launcher=0x7f020000;
|
||||
}
|
||||
public static final class id {
|
||||
public static final int action_settings=0x7f080000;
|
||||
}
|
||||
public static final class layout {
|
||||
public static final int activity_main=0x7f030000;
|
||||
}
|
||||
public static final class menu {
|
||||
public static final int main=0x7f070000;
|
||||
}
|
||||
public static final class string {
|
||||
public static final int action_settings=0x7f050001;
|
||||
public static final int app_name=0x7f050000;
|
||||
public static final int hello_world=0x7f050002;
|
||||
}
|
||||
public static final class style {
|
||||
/**
|
||||
Base application theme, dependent on API level. This theme is replaced
|
||||
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
|
||||
|
||||
|
||||
Theme customizations available in newer API levels can go in
|
||||
res/values-vXX/styles.xml, while customizations related to
|
||||
backward-compatibility can go here.
|
||||
|
||||
|
||||
Base application theme for API 11+. This theme completely replaces
|
||||
AppBaseTheme from res/values/styles.xml on API 11+ devices.
|
||||
|
||||
API 11 theme customizations can go here.
|
||||
|
||||
Base application theme for API 14+. This theme completely replaces
|
||||
AppBaseTheme from BOTH res/values/styles.xml and
|
||||
res/values-v11/styles.xml on API 14+ devices.
|
||||
|
||||
API 14 theme customizations can go here.
|
||||
*/
|
||||
public static final int AppBaseTheme=0x7f060000;
|
||||
/** Application theme.
|
||||
All customizations that are NOT specific to a particular API-level can go here.
|
||||
*/
|
||||
public static final int AppTheme=0x7f060001;
|
||||
}
|
||||
}
|
BIN
platforms/mtk/SPPClient/ic_launcher-web.png
Normal file
BIN
platforms/mtk/SPPClient/ic_launcher-web.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 50 KiB |
BIN
platforms/mtk/SPPClient/libs/android-support-v4.jar
Normal file
BIN
platforms/mtk/SPPClient/libs/android-support-v4.jar
Normal file
Binary file not shown.
20
platforms/mtk/SPPClient/proguard-project.txt
Normal file
20
platforms/mtk/SPPClient/proguard-project.txt
Normal file
@ -0,0 +1,20 @@
|
||||
# To enable ProGuard in your project, edit project.properties
|
||||
# to define the proguard.config property as described in that file.
|
||||
#
|
||||
# Add project specific ProGuard rules here.
|
||||
# By default, the flags in this file are appended to flags specified
|
||||
# in ${sdk.dir}/tools/proguard/proguard-android.txt
|
||||
# You can edit the include path and order by changing the ProGuard
|
||||
# include property in project.properties.
|
||||
#
|
||||
# For more details, see
|
||||
# http://developer.android.com/guide/developing/tools/proguard.html
|
||||
|
||||
# Add any project specific keep options here:
|
||||
|
||||
# If your project uses WebView with JS, uncomment the following
|
||||
# and specify the fully qualified class name to the JavaScript interface
|
||||
# class:
|
||||
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
|
||||
# public *;
|
||||
#}
|
14
platforms/mtk/SPPClient/project.properties
Normal file
14
platforms/mtk/SPPClient/project.properties
Normal file
@ -0,0 +1,14 @@
|
||||
# This file is automatically generated by Android Tools.
|
||||
# Do not modify this file -- YOUR CHANGES WILL BE ERASED!
|
||||
#
|
||||
# This file must be checked in Version Control Systems.
|
||||
#
|
||||
# To customize properties used by the Ant build system edit
|
||||
# "ant.properties", and override values to adapt the script to your
|
||||
# project structure.
|
||||
#
|
||||
# To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home):
|
||||
#proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt
|
||||
|
||||
# Project target.
|
||||
target=android-19
|
BIN
platforms/mtk/SPPClient/res/drawable-hdpi/ic_launcher.png
Normal file
BIN
platforms/mtk/SPPClient/res/drawable-hdpi/ic_launcher.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 7.5 KiB |
BIN
platforms/mtk/SPPClient/res/drawable-mdpi/ic_launcher.png
Normal file
BIN
platforms/mtk/SPPClient/res/drawable-mdpi/ic_launcher.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 3.7 KiB |
BIN
platforms/mtk/SPPClient/res/drawable-xhdpi/ic_launcher.png
Normal file
BIN
platforms/mtk/SPPClient/res/drawable-xhdpi/ic_launcher.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 12 KiB |
BIN
platforms/mtk/SPPClient/res/drawable-xxhdpi/ic_launcher.png
Normal file
BIN
platforms/mtk/SPPClient/res/drawable-xxhdpi/ic_launcher.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 24 KiB |
16
platforms/mtk/SPPClient/res/layout/activity_main.xml
Normal file
16
platforms/mtk/SPPClient/res/layout/activity_main.xml
Normal file
@ -0,0 +1,16 @@
|
||||
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:paddingBottom="@dimen/activity_vertical_margin"
|
||||
android:paddingLeft="@dimen/activity_horizontal_margin"
|
||||
android:paddingRight="@dimen/activity_horizontal_margin"
|
||||
android:paddingTop="@dimen/activity_vertical_margin"
|
||||
tools:context=".MainActivity" >
|
||||
|
||||
<TextView
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="@string/hello_world" />
|
||||
|
||||
</RelativeLayout>
|
9
platforms/mtk/SPPClient/res/menu/main.xml
Normal file
9
platforms/mtk/SPPClient/res/menu/main.xml
Normal file
@ -0,0 +1,9 @@
|
||||
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
|
||||
|
||||
<item
|
||||
android:id="@+id/action_settings"
|
||||
android:orderInCategory="100"
|
||||
android:showAsAction="never"
|
||||
android:title="@string/action_settings"/>
|
||||
|
||||
</menu>
|
8
platforms/mtk/SPPClient/res/values-sw600dp/dimens.xml
Normal file
8
platforms/mtk/SPPClient/res/values-sw600dp/dimens.xml
Normal file
@ -0,0 +1,8 @@
|
||||
<resources>
|
||||
|
||||
<!--
|
||||
Customize dimensions originally defined in res/values/dimens.xml (such as
|
||||
screen margins) for sw600dp devices (e.g. 7" tablets) here.
|
||||
-->
|
||||
|
||||
</resources>
|
@ -0,0 +1,9 @@
|
||||
<resources>
|
||||
|
||||
<!--
|
||||
Customize dimensions originally defined in res/values/dimens.xml (such as
|
||||
screen margins) for sw720dp devices (e.g. 10" tablets) in landscape here.
|
||||
-->
|
||||
<dimen name="activity_horizontal_margin">128dp</dimen>
|
||||
|
||||
</resources>
|
11
platforms/mtk/SPPClient/res/values-v11/styles.xml
Normal file
11
platforms/mtk/SPPClient/res/values-v11/styles.xml
Normal file
@ -0,0 +1,11 @@
|
||||
<resources>
|
||||
|
||||
<!--
|
||||
Base application theme for API 11+. This theme completely replaces
|
||||
AppBaseTheme from res/values/styles.xml on API 11+ devices.
|
||||
-->
|
||||
<style name="AppBaseTheme" parent="android:Theme.Holo.Light">
|
||||
<!-- API 11 theme customizations can go here. -->
|
||||
</style>
|
||||
|
||||
</resources>
|
12
platforms/mtk/SPPClient/res/values-v14/styles.xml
Normal file
12
platforms/mtk/SPPClient/res/values-v14/styles.xml
Normal file
@ -0,0 +1,12 @@
|
||||
<resources>
|
||||
|
||||
<!--
|
||||
Base application theme for API 14+. This theme completely replaces
|
||||
AppBaseTheme from BOTH res/values/styles.xml and
|
||||
res/values-v11/styles.xml on API 14+ devices.
|
||||
-->
|
||||
<style name="AppBaseTheme" parent="android:Theme.Holo.Light.DarkActionBar">
|
||||
<!-- API 14 theme customizations can go here. -->
|
||||
</style>
|
||||
|
||||
</resources>
|
7
platforms/mtk/SPPClient/res/values/dimens.xml
Normal file
7
platforms/mtk/SPPClient/res/values/dimens.xml
Normal file
@ -0,0 +1,7 @@
|
||||
<resources>
|
||||
|
||||
<!-- Default screen margins, per the Android Design guidelines. -->
|
||||
<dimen name="activity_horizontal_margin">16dp</dimen>
|
||||
<dimen name="activity_vertical_margin">16dp</dimen>
|
||||
|
||||
</resources>
|
8
platforms/mtk/SPPClient/res/values/strings.xml
Normal file
8
platforms/mtk/SPPClient/res/values/strings.xml
Normal file
@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
|
||||
<string name="app_name">SPPClient</string>
|
||||
<string name="action_settings">Settings</string>
|
||||
<string name="hello_world">Hello world!</string>
|
||||
|
||||
</resources>
|
20
platforms/mtk/SPPClient/res/values/styles.xml
Normal file
20
platforms/mtk/SPPClient/res/values/styles.xml
Normal file
@ -0,0 +1,20 @@
|
||||
<resources>
|
||||
|
||||
<!--
|
||||
Base application theme, dependent on API level. This theme is replaced
|
||||
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
|
||||
-->
|
||||
<style name="AppBaseTheme" parent="android:Theme.Light">
|
||||
<!--
|
||||
Theme customizations available in newer API levels can go in
|
||||
res/values-vXX/styles.xml, while customizations related to
|
||||
backward-compatibility can go here.
|
||||
-->
|
||||
</style>
|
||||
|
||||
<!-- Application theme. -->
|
||||
<style name="AppTheme" parent="AppBaseTheme">
|
||||
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
|
||||
</style>
|
||||
|
||||
</resources>
|
@ -0,0 +1,103 @@
|
||||
package com.bluekitchen.btstack;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
|
||||
import android.net.LocalSocket;
|
||||
import android.net.LocalSocketAddress;
|
||||
|
||||
public class SocketConnectionUnix extends SocketConnection {
|
||||
|
||||
private LocalSocket socket;
|
||||
private String unixSocketName = "/data/btstack/BTstack";
|
||||
private InputStream in;
|
||||
private OutputStream out;
|
||||
private byte inHeader[] = new byte[6];
|
||||
private byte inPayload[] = new byte[2000];
|
||||
|
||||
public SocketConnectionUnix(){
|
||||
socket = null;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see com.bluekitchen.btstack.SocketConnection#connect()
|
||||
*/
|
||||
@Override
|
||||
public boolean connect() {
|
||||
try {
|
||||
socket = new LocalSocket();
|
||||
LocalSocketAddress socketAddress = new LocalSocketAddress(unixSocketName, LocalSocketAddress.Namespace.FILESYSTEM);
|
||||
socket.connect(socketAddress);
|
||||
in = socket.getInputStream();
|
||||
out = socket.getOutputStream();
|
||||
return true;
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see com.bluekitchen.btstack.SocketConnection#sendPacket(com.bluekitchen.btstack.Packet)
|
||||
*/
|
||||
@Override
|
||||
public boolean sendPacket(Packet packet) {
|
||||
|
||||
if (out == null) return false;
|
||||
|
||||
try {
|
||||
System.out.println("Send "); Util.hexdump(packet.getBuffer(), packet.getPayloadLen());
|
||||
out.write(headerForPacket(packet));
|
||||
out.write(packet.getBuffer());
|
||||
out.flush();
|
||||
return true;
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see com.bluekitchen.btstack.SocketConnection#receivePacket()
|
||||
*/
|
||||
@Override
|
||||
public Packet receivePacket() {
|
||||
|
||||
if (in == null) return null;
|
||||
|
||||
int bytes_read = Util.readExactly(in, inHeader, 0, 6);
|
||||
if (bytes_read != 6) return null;
|
||||
|
||||
int packetType = Util.readBt16(inHeader, 0);
|
||||
int channel = Util.readBt16(inHeader, 2);
|
||||
int len = Util.readBt16(inHeader, 4);
|
||||
|
||||
Util.readExactly(in, inPayload, 0, len);
|
||||
|
||||
Packet packet = new Packet(packetType, channel ,inPayload, len);
|
||||
return packet;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see com.bluekitchen.btstack.SocketConnection#disconnect()
|
||||
*/
|
||||
@Override
|
||||
public void disconnect() {
|
||||
|
||||
if (socket != null){
|
||||
try {
|
||||
socket.close();
|
||||
} catch (IOException e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private byte[] headerForPacket(Packet packet) {
|
||||
byte header[] = new byte[6];
|
||||
Util.storeBt16(header, 0, packet.getPacketType());
|
||||
Util.storeBt16(header, 2, packet.getChannel());
|
||||
Util.storeBt16(header, 4, packet.getBuffer().length);
|
||||
return header;
|
||||
}
|
||||
}
|
@ -0,0 +1,400 @@
|
||||
package com.bluekitchen.sppclient;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.app.Activity;
|
||||
import android.os.Bundle;
|
||||
import android.util.Log;
|
||||
import android.view.Menu;
|
||||
import android.widget.TextView;
|
||||
|
||||
import com.bluekitchen.btstack.RFCOMMDataPacket;
|
||||
import com.bluekitchen.btstack.BD_ADDR;
|
||||
import com.bluekitchen.btstack.BTstack;
|
||||
import com.bluekitchen.btstack.Packet;
|
||||
import com.bluekitchen.btstack.PacketHandler;
|
||||
import com.bluekitchen.btstack.Util;
|
||||
import com.bluekitchen.btstack.event.BTstackEventState;
|
||||
import com.bluekitchen.btstack.event.HCIEventCommandComplete;
|
||||
import com.bluekitchen.btstack.event.HCIEventDisconnectionComplete;
|
||||
import com.bluekitchen.btstack.event.HCIEventHardwareError;
|
||||
import com.bluekitchen.btstack.event.HCIEventInquiryComplete;
|
||||
import com.bluekitchen.btstack.event.HCIEventInquiryResultWithRssi;
|
||||
import com.bluekitchen.btstack.event.HCIEventRemoteNameRequestComplete;
|
||||
import com.bluekitchen.btstack.event.RFCOMMEventOpenChannelComplete;
|
||||
import com.bluekitchen.btstack.event.SDPQueryComplete;
|
||||
import com.bluekitchen.btstack.event.SDPQueryRFCOMMService;
|
||||
|
||||
|
||||
public class MainActivity extends Activity implements PacketHandler {
|
||||
|
||||
// minimal Android text UI
|
||||
|
||||
private static final String BTSTACK_TAG = "BTstack";
|
||||
|
||||
private TextView tv;
|
||||
private String onScreenMessage = "";
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_main);
|
||||
|
||||
tv = new TextView(this);
|
||||
setContentView(tv);
|
||||
|
||||
test();
|
||||
}
|
||||
|
||||
void addMessage(final String message){
|
||||
onScreenMessage = onScreenMessage + "\n" + message;
|
||||
Log.d(BTSTACK_TAG, message);
|
||||
runOnUiThread(new Runnable(){
|
||||
public void run(){
|
||||
tv.setText(onScreenMessage);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void addTempMessage(final String message){
|
||||
Log.d(BTSTACK_TAG, message);
|
||||
runOnUiThread(new Runnable(){
|
||||
public void run(){
|
||||
tv.setText(onScreenMessage +"\n" + message);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void clearMessages(){
|
||||
onScreenMessage = "";
|
||||
runOnUiThread(new Runnable(){
|
||||
public void run(){
|
||||
tv.setText(onScreenMessage);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean onCreateOptionsMenu(Menu menu) {
|
||||
// Inflate the menu; this adds items to the action bar if it is present.
|
||||
getMenuInflater().inflate(R.menu.main, menu);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
// helper class to store inquiry results
|
||||
private static class RemoteDevice{
|
||||
private enum NAME_STATE {
|
||||
REMOTE_NAME_REQUEST, REMOTE_NAME_INQUIRED, REMOTE_NAME_FETCHED
|
||||
};
|
||||
|
||||
private BD_ADDR bdAddr;
|
||||
private int pageScanRepetitionMode;
|
||||
private int clockOffset;
|
||||
private String name;
|
||||
private NAME_STATE state;
|
||||
|
||||
public RemoteDevice(BD_ADDR bdAddr, int pageScanRepetitionMode, int clockOffset) {
|
||||
this.bdAddr = bdAddr;
|
||||
this.state = NAME_STATE.REMOTE_NAME_REQUEST;
|
||||
}
|
||||
|
||||
public boolean nameRequest() {
|
||||
return this.state == NAME_STATE.REMOTE_NAME_REQUEST;
|
||||
}
|
||||
|
||||
public void inquireName(BTstack btstack) {
|
||||
this.state = NAME_STATE.REMOTE_NAME_INQUIRED;
|
||||
btstack.HCIRemoteNameRequest(bdAddr, pageScanRepetitionMode, 0, clockOffset);
|
||||
}
|
||||
|
||||
public BD_ADDR getBDAddress() {
|
||||
return this.bdAddr;
|
||||
}
|
||||
|
||||
private String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
private void setName(String name) {
|
||||
this.state = NAME_STATE.REMOTE_NAME_FETCHED;
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
|
||||
// constants
|
||||
|
||||
private static final int HCI_INQUIRY_LAP = 0x9E8B33; // 0x9E8B33: General/Unlimited Inquiry Access Code (GIAC)
|
||||
private static final int INQUIRY_INTERVAL = 5;
|
||||
private static final int SPP_UUID = 0x1002;
|
||||
|
||||
private enum STATE {
|
||||
w4_btstack_working, w4_write_inquiry_mode, w4_scan_result, w4_remote_name, w4_sdp_query_result, w4_connected, active
|
||||
};
|
||||
|
||||
private static final String REMOTE_DEVICE_NAME_PREFIX = "BTstack SPP";
|
||||
private static final String RFCOMM_SERVICE_PREFIX = "SPP";
|
||||
|
||||
// state
|
||||
|
||||
private BTstack btstack;
|
||||
private STATE state;
|
||||
private int testHandle;
|
||||
|
||||
private int rfcommChannelID = 0;
|
||||
private int mtu = 0;
|
||||
private BD_ADDR remoteBDAddr;
|
||||
|
||||
List<SDPQueryRFCOMMService> services = new ArrayList<SDPQueryRFCOMMService>(10);
|
||||
List<RemoteDevice> devices = new ArrayList<RemoteDevice>(10);
|
||||
|
||||
private int counter;
|
||||
|
||||
|
||||
private boolean hasMoreRemoteNameRequests() {
|
||||
for (RemoteDevice device:devices){
|
||||
if (device.nameRequest()){
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private void doNextRemoteNameRequest() {
|
||||
for (RemoteDevice device:devices){
|
||||
if (device.nameRequest()){
|
||||
addMessage("Get remote name of " + device.getBDAddress());
|
||||
device.inquireName(btstack);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void restartInquiry(){
|
||||
clearMessages();
|
||||
addMessage("Restart Inquiry");
|
||||
state = STATE.w4_scan_result;
|
||||
services.clear();
|
||||
devices.clear();
|
||||
counter = 0;
|
||||
btstack.HCIInquiry(HCI_INQUIRY_LAP, INQUIRY_INTERVAL, 0);
|
||||
}
|
||||
|
||||
@SuppressLint("DefaultLocale")
|
||||
public void handlePacket(Packet packet){
|
||||
if (packet instanceof HCIEventHardwareError){
|
||||
clearMessages();
|
||||
addMessage("Received HCIEventHardwareError, \nhandle power cycle of the Bluetooth \nchip of the device.");
|
||||
}
|
||||
|
||||
if (packet instanceof HCIEventDisconnectionComplete){
|
||||
HCIEventDisconnectionComplete event = (HCIEventDisconnectionComplete) packet;
|
||||
testHandle = event.getConnectionHandle();
|
||||
addMessage(String.format("Received disconnect, status %d, handle %x", event.getStatus(), testHandle));
|
||||
restartInquiry();
|
||||
return;
|
||||
}
|
||||
|
||||
switch (state){
|
||||
case w4_btstack_working:
|
||||
if (packet instanceof BTstackEventState){
|
||||
BTstackEventState event = (BTstackEventState) packet;
|
||||
if (event.getState() == 2) {
|
||||
addMessage("BTstack working. Set write inquiry mode.");
|
||||
state = STATE.w4_write_inquiry_mode;
|
||||
btstack.HCIWriteInquiryMode(1); // with RSSI
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case w4_write_inquiry_mode:
|
||||
if (packet instanceof HCIEventCommandComplete){
|
||||
state = STATE.w4_scan_result;
|
||||
btstack.HCIInquiry(HCI_INQUIRY_LAP, INQUIRY_INTERVAL, 0);
|
||||
}
|
||||
break;
|
||||
|
||||
case w4_scan_result:
|
||||
if (packet instanceof HCIEventInquiryResultWithRssi){
|
||||
HCIEventInquiryResultWithRssi result = (HCIEventInquiryResultWithRssi) packet;
|
||||
devices.add(new RemoteDevice(result.getBdAddr(), result.getPageScanRepetitionMode(), result.getClockOffset()));
|
||||
addMessage("Found device: " + result.getBdAddr());
|
||||
}
|
||||
|
||||
if (packet instanceof HCIEventInquiryComplete){
|
||||
state = STATE.w4_remote_name;
|
||||
if (hasMoreRemoteNameRequests()){
|
||||
doNextRemoteNameRequest();
|
||||
break;
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case w4_remote_name:
|
||||
if (packet instanceof HCIEventRemoteNameRequestComplete){
|
||||
HCIEventRemoteNameRequestComplete result = (HCIEventRemoteNameRequestComplete) packet;
|
||||
if (result.getStatus() == 0){
|
||||
// store name on success
|
||||
setNameForDeviceWithBDAddr(result.getRemoteName(), result.getBdAddr());
|
||||
}
|
||||
|
||||
if (hasMoreRemoteNameRequests()){
|
||||
doNextRemoteNameRequest();
|
||||
break;
|
||||
}
|
||||
|
||||
// discovery done, connect to device with remote name prefix
|
||||
|
||||
RemoteDevice remoteDevice = null;
|
||||
for (RemoteDevice device : devices){
|
||||
if (device.getName() != null && device.getName().startsWith(REMOTE_DEVICE_NAME_PREFIX)){
|
||||
remoteDevice = device;
|
||||
}
|
||||
}
|
||||
|
||||
// try first one otherwise
|
||||
if (remoteDevice == null && devices.size() > 0){
|
||||
remoteDevice = devices.get(0);
|
||||
}
|
||||
|
||||
// no device, restart inquiry
|
||||
if (remoteDevice == null){
|
||||
restartInquiry();
|
||||
break;
|
||||
}
|
||||
|
||||
// start SDP query for RFCOMMM services
|
||||
remoteBDAddr = remoteDevice.getBDAddress();
|
||||
if (remoteDevice.getName() == null){
|
||||
addMessage("Start SDP Query of " + remoteDevice.getBDAddress());
|
||||
} else {
|
||||
addMessage("Start SDP Query of " + remoteDevice.getName());
|
||||
}
|
||||
state = STATE.w4_sdp_query_result;
|
||||
byte[] serviceSearchPattern = Util.serviceSearchPatternForUUID16(SPP_UUID);
|
||||
btstack.SDPClientQueryRFCOMMServices(remoteBDAddr, serviceSearchPattern);
|
||||
break;
|
||||
}
|
||||
break;
|
||||
|
||||
case w4_sdp_query_result:
|
||||
if (packet instanceof SDPQueryRFCOMMService){
|
||||
SDPQueryRFCOMMService service = (SDPQueryRFCOMMService) packet;
|
||||
services.add(service);
|
||||
addMessage(String.format("Found \"%s\", channel %d", service.getName(), service.getRFCOMMChannel()));
|
||||
}
|
||||
if (packet instanceof SDPQueryComplete){
|
||||
// find service with "SPP" prefix
|
||||
SDPQueryRFCOMMService selectedService = null;
|
||||
for (SDPQueryRFCOMMService service : services){
|
||||
if (service.getName().startsWith(RFCOMM_SERVICE_PREFIX)){
|
||||
selectedService = service;
|
||||
break;
|
||||
}
|
||||
}
|
||||
// restart demo, if no service with prefix found
|
||||
if (selectedService == null){
|
||||
restartInquiry();
|
||||
break;
|
||||
}
|
||||
|
||||
// connect
|
||||
state = STATE.w4_connected;
|
||||
clearMessages();
|
||||
addMessage("SPP Test Application / Part 2");
|
||||
addMessage("Connect to channel nr " + selectedService.getRFCOMMChannel());
|
||||
btstack.RFCOMMCreateChannel(remoteBDAddr, selectedService.getRFCOMMChannel());
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case w4_connected:
|
||||
if (packet instanceof RFCOMMEventOpenChannelComplete){
|
||||
RFCOMMEventOpenChannelComplete e = (RFCOMMEventOpenChannelComplete) packet;
|
||||
if (e.getStatus() != 0) {
|
||||
addMessage("RFCOMM channel open failed, status " + e.getStatus());
|
||||
} else {
|
||||
state = STATE.active;
|
||||
rfcommChannelID = e.getRFCOMMCid();
|
||||
mtu = e.getMaxFrameSize();
|
||||
addMessage(String.format("RFCOMM channel open succeeded. \nNew RFCOMM Channel ID %d,\n max frame size %d", rfcommChannelID, mtu));
|
||||
|
||||
counter = 0;
|
||||
new Thread(new Runnable(){
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
while(state == STATE.active){
|
||||
Thread.sleep(1000);
|
||||
byte[] data;
|
||||
try {
|
||||
data = String.format("BTstack SPP Counter %d\n", counter).getBytes("utf8");
|
||||
if (counter < Integer.MAX_VALUE){
|
||||
counter++;
|
||||
} else {
|
||||
counter = 0;
|
||||
}
|
||||
btstack.RFCOMMSendData(rfcommChannelID, data);
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
} catch (InterruptedException e) {}
|
||||
}
|
||||
}).start();
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case active:
|
||||
if (packet instanceof RFCOMMDataPacket){
|
||||
addTempMessage("Received RFCOMM data packet: \n" + packet.toString());
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private RemoteDevice deviceForBDAddr(BD_ADDR bdAddr){
|
||||
for (RemoteDevice device:devices){
|
||||
if (device.getBDAddress().equals(bdAddr) )
|
||||
return device;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private void setNameForDeviceWithBDAddr(String remoteName, BD_ADDR bdAddr) {
|
||||
RemoteDevice device = deviceForBDAddr(bdAddr);
|
||||
if (device != null){
|
||||
addMessage("Found " + remoteName);
|
||||
device.setName(remoteName);
|
||||
}
|
||||
}
|
||||
|
||||
void test(){
|
||||
counter = 0;
|
||||
addMessage("SPP Test Application");
|
||||
|
||||
btstack = new BTstack();
|
||||
btstack.registerPacketHandler(this);
|
||||
|
||||
boolean ok = btstack.connect();
|
||||
if (!ok) {
|
||||
addMessage("Failed to connect to BTstack Server");
|
||||
return;
|
||||
}
|
||||
|
||||
addMessage("BTstackSetPowerMode(1)");
|
||||
|
||||
state = STATE.w4_btstack_working;
|
||||
btstack.BTstackSetPowerMode(1);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user