I'm hosting a server on my raspberry pi 2, and sending a stream of values between 0 and 360. I'm trying to grab whatever number is just sent on my android phone. Currently, it runs through the ReadServer code once, returns 48 (char 0) and then crashes with an 'unhandled runtime exception'. I'm able to just dummy values and have it work perfectly fine, so I know its something having to do with the client/server side.
Yes I have internet permission specified in my manifest.
PI
Server hosted on raspberry pi 2:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <unistd.h>
void error(char * msg)
{
perror(msg);
exit(1);
}
int main(int argc, char *argv[])
{
int sockfd, clisockfd, portno = 5003;
socklen_t clilen;
char buffer[127];
struct sockaddr_in serv_addr, cli_addr;
int n;
/* call socket */
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) { error("ERROR opening socket"); }
/* initialize socket structure */
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno);
printf("waiting to bind\n");
fflush(stdout);
if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
{ error("ERROR on binding"); }
listen(sockfd, 5);
clilen = (socklen_t) sizeof(cli_addr);
printf("waiting to accept\n");
clisockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
printf("accepted\n");
if (clisockfd < 0) { error("ERROR on accept"); }
int heading = 0;
while (heading < 360)
{
bzero(buffer, 126);
printf("sending\n");
sprintf(buffer, "%d\n", heading);
n = write(clisockfd, buffer, strlen(buffer));
if (n < 0) { error("ERROR writing to socket"); }
printf("sending %s\n", buffer);
usleep(2 * 1000*1000);
heading = heading + 2;
}
close(sockfd);
return 0;
}
ANDROID
Main activity code:
public class MainActivity extends AppCompatActivity {
final String IP_ADDRESS = "x.x.x.x"; // (is an actual address)
final int PORT = 5003;
boolean isManual = true;
RelativeLayout layout_joystick;
JoyStickClass js;
TextView velocityTextView;
Button manualButton;
ImageView compassPhoto;
Wifi wifiController;
ReadServer serverObject;
Timer serverTimer = new Timer();
Handler serverPollHandler;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// set new view
setContentView(R.layout.activity_main);
// define buttons/interactions
manualButton = (Button) findViewById(R.id.manual_button);
Button disconnectButton = (Button) findViewById(R.id.disconnect_button);
layout_joystick = (RelativeLayout) findViewById(R.id.layout_joystick);
compassPhoto = (ImageView) findViewById(R.id.imageViewCompass);
// set joystick settings
js = new JoyStickClass(getApplicationContext()
, layout_joystick, R.drawable.image_button);
js.setStickSize(150, 150);
js.setLayoutSize(500, 500);
js.setLayoutAlpha(150);
js.setStickAlpha(90);
js.setOffset(90);
js.setMinimumDistance(50);
// set button listeners
manualButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
toggleMode();
}
});
layout_joystick.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View arg0, MotionEvent arg1) {
return jsOnTouchListener(arg0, arg1);
}
});
disconnectButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
switchToLoadScreen(view);
}
});
// load camera feed
WebView wv = (WebView)findViewById(R.id.webView);
velocityTextView = (TextView) findViewById(R.id.velocityText);
serverPollHandler = new Handler(serverCallback);
wv.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
//connect wifi
wifiController = new Wifi(IP_ADDRESS, PORT);
if (!wifiController.Connect()){
switchToLoadScreen(findViewById(android.R.id.content));
}
serverObject = new ReadServer(wifiController.getInputStream());
serverTimer.scheduleAtFixedRate(new getFromServer(), 500, 2 * 1000);
}
});
wv.loadUrl("file:///android_asset/cameraFeed.html");
}
Handler.Callback serverCallback = new Handler.Callback() {
public boolean handleMessage(Message msg) {
velocityTextView.setText("1m/s");
try {
serverObject.execute().get();
int headingToMoveTo = serverObject.getHeading();
moveCompassTo(headingToMoveTo);
}
catch (InterruptedException e) {}
catch (ExecutionException e) {}
return true;
}
};
// move the compass
private void moveCompassTo(float degree) {
float currentDegree = 0f;
// create a rotation animation (reverse turn degree degrees)
RotateAnimation ra = new RotateAnimation(
currentDegree,
-degree,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF,
0.5f);
// how long the animation will take place
ra.setDuration(5);
// set the animation after the end of the reservation status
ra.setFillAfter(true);
// Start the animation
compassPhoto.startAnimation(ra);
int a = 5;
}
// toggle between manual and autonomous modes
private void toggleMode() {
if (isManual) {
// set Autonomous Mode
manualButton.setText(R.string.manual_text);
layout_joystick.setVisibility(View.INVISIBLE);
wifiController.enterAutonomous();
isManual = false;
} else {
// set Manual Mode
manualButton.setText(R.string.autonomous_text);
layout_joystick.setVisibility(View.VISIBLE);
wifiController.enterManual();
isManual = true;
}
}
// switch between views
private void switchToLoadScreen(View thisView) {
if (wifiController.Disconnect()) {
serverTimer.cancel();
Intent myIntent = new Intent(thisView.getContext(), LoadScreen.class);
startActivityForResult(myIntent, 0);
}
}
// control what moving the joystick does {emitted}
private boolean jsOnTouchListener(View arg0, MotionEvent arg1) {
js.drawStick(arg1);
if (arg1.getAction() == MotionEvent.ACTION_DOWN
|| arg1.getAction() == MotionEvent.ACTION_MOVE) {
// textView1.setText("X : " + String.valueOf(js.getX()));
// textView2.setText("Y : " + String.valueOf(js.getY()));
// textView3.setText("Angle : " + String.valueOf(js.getAngle()));
// textView4.setText("Distance : " + String.valueOf(js.getDistance()));
switch (js.get8Direction()){
case JoyStickClass.STICK_UP:
wifiController.moveForward();
break;
case JoyStickClass.STICK_UPRIGHT:
wifiController.moveForwardRight();
break;
case JoyStickClass.STICK_RIGHT:
wifiController.moveRight();
break;
case JoyStickClass.STICK_DOWNRIGHT:
wifiController.moveBackRight();
break;
case JoyStickClass.STICK_DOWN:
wifiController.moveBack();
break;
case JoyStickClass.STICK_DOWNLEFT:
wifiController.moveBackLeft();
break;
case JoyStickClass.STICK_LEFT:
wifiController.moveLeft();
break;
case JoyStickClass.STICK_UPLEFT:
wifiController.moveForwardLeft();
break;
case JoyStickClass.STICK_NONE: // center
break;
}
} else if (arg1.getAction() == MotionEvent.ACTION_UP) {
// textView1.setText("X :");
// textView2.setText("Y :");
// textView3.setText("Angle :");
// textView4.setText("Distance :");
// textView5.setText("Direction :");
}
return true;
}
public class getFromServer extends TimerTask {
public void run() {
serverPollHandler.sendEmptyMessage(0);
}
}
}
ReadServer code:
public class ReadServer extends AsyncTask {
BufferedReader thisStream;
int heading = 0;
public ReadServer(BufferedReader fromPi)
{
thisStream = fromPi;
}
public int getHeading() {
return heading;
}
#Override
protected Object doInBackground(Object[] params) {
//communication from PI
try {
if (thisStream.ready()) {
int read = thisStream.read();
String t = Character.toString ((char) read);
if (read > 0) {
heading = Integer.parseInt(t);
}
}
}
catch (IOException e) {}
return params;
}
}
WifiController code:
public class Wifi extends AsyncTask {
// THESE MUST BE IDENTICAL TO THE PI CODE FOR CORRECT FUNCTIONALITY
private enum send_modes {
AUTONOMOUS_FLAG,
MANUAL_FLAG,
DISCONNECT_PI,
MOVE_NORTH,
MOVE_NORTHEAST,
MOVE_EAST,
MOVE_SOUTHEAST,
MOVE_SOUTH,
MOVE_SOUTHWEST,
MOVE_WEST,
MOVE_NORTHWEST
}
//
private Socket phoneToPiSocket;
private boolean isConnected = false;
private InetAddress piIPAddress;
private PrintWriter piSocketWriter;
private int portNumber;
private BufferedReader fromPi;
public Wifi(String ipAddr, int port) {
try {
portNumber = port;
piIPAddress = InetAddress.getByName(ipAddr);
} catch (UnknownHostException e) {
piIPAddress = null;
}
}
#Override
protected Object doInBackground(Object[] params) {
try {
phoneToPiSocket = new Socket(piIPAddress, portNumber);
fromPi = new BufferedReader(new InputStreamReader (phoneToPiSocket.getInputStream()));
// piSocketWriter = new PrintWriter(phoneToPiSocket.getOutputStream(), true);
isConnected = true;
} catch (ConnectException e) {
isConnected = false;
} catch (UnknownHostException e) {
isConnected = false;
} catch (IOException e) {
isConnected = false;
}
return params;
}
public boolean Disconnect() {
if (isConnected) {
try {
fromPi.close();
//disconnectFromPi(); // has to happen before writer is closed!
//piSocketWriter.close();
phoneToPiSocket.close();
isConnected = false;
} catch (IOException e) {
return false;
}
}
return true;
}
public BufferedReader getInputStream()
{
return fromPi;
}
public boolean Connect() {
// set up wifi connection
try {
execute().get();
} catch (InterruptedException e) {
} catch (ExecutionException e) {
}
if (isConnected) {
return true;
} else {
return false;
}
}
boolean isConnected(){
return isConnected;
}
//communication to PI
public void enterAutonomous()
{
piSocketWriter.println(send_modes.AUTONOMOUS_FLAG.ordinal());
}
public void enterManual()
{
piSocketWriter.println(send_modes.MANUAL_FLAG.ordinal());
}
public void disconnectFromPi()
{
piSocketWriter.println(send_modes.DISCONNECT_PI.ordinal());
}
public void moveForward()
{
piSocketWriter.println(send_modes.MOVE_NORTH.ordinal());
}
public void moveForwardRight()
{
piSocketWriter.println(send_modes.MOVE_NORTHEAST.ordinal());
}
public void moveRight()
{
piSocketWriter.println(send_modes.MOVE_EAST.ordinal());
}
public void moveBackRight()
{
piSocketWriter.println(send_modes.MOVE_SOUTHEAST.ordinal());
}
public void moveBack()
{
piSocketWriter.println(send_modes.MOVE_SOUTH.ordinal());
}
public void moveBackLeft()
{
piSocketWriter.println(send_modes.MOVE_SOUTHWEST.ordinal());
}
public void moveLeft()
{
piSocketWriter.println(send_modes.MOVE_WEST.ordinal());
}
public void moveForwardLeft()
{
piSocketWriter.println(send_modes.MOVE_NORTHWEST.ordinal());
}
}
I apologize for how messy / horribly named everything is. Its a work in progress.
Thanks for the help.
UPDATED INFO:
The exception i get is:
"main#20747" prio=5 waiting
java.lang.Thread.State: WAITING
at com.android.internal.os.RuntimeInit$UncaughtHandler.uncaughtException(RuntimeInit.java:72)
at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:693)
at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:690)
Picture of frames at crash
Related
I am trying to send data over bluetooth to stm32f411 board. I am using HC-05 v2 bluetooth module which communicates over UART interface. When I'm sending data from computer it works perfectly- data is sent and STM's response is received over UART in RealTerm. However switching to bluetooth module and sending and receiving data from android app doesn't work as expected. For now I want to send ASCII data (for now just 0 and 1, but the goal is to send integers from 0 to 100). Perhaps it has something to do with message length or app's baud rate.
I also tried to send data as byte not byte array but it didn't work- when I sent 0 it worked fine but when I sent 1 I got 120 decimal on STM, 2 I got 128, 3 I got 248. When I tried sending the same data back to app every message started with [B# followed by usually 7 characters.
My android studio code:
public class MainActivity extends AppCompatActivity {
Button someButton;
ListView pairedDevListView;
Switch bluetoothSwitch;
BluetoothAdapter myBluetoothAdapter;
Intent bluetoothEnablingIntent;
TextView connectionStatusText;
SeekBar rightEnSeekBar, leftEnSeekBar;
SendReceive sendReceive;
BluetoothDevice[] bluetoothPairedDevArray;
int REQUEST_ENABLE_BLUETOOTH = 1;
int requestCodeForEnable;
static final int STATE_LISTENING = 1;
static final int STATE_CONNECTING = 2;
static final int STATE_CONNECTED = 3;
static final int STATE_CONNECTION_FAILED = 4;
static final int STATE_MESSAGE_RECEIVED = 5;
private static final String APP_NAME = "STM32";
private static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb");
Handler handler = new Handler(new Handler.Callback() {
#Override
public boolean handleMessage(#NonNull Message message) {
switch (message.what){
case STATE_LISTENING:
connectionStatusText.setText("Listening");
break;
case STATE_CONNECTING:
connectionStatusText.setText("Connecting");
break;
case STATE_CONNECTED:
connectionStatusText.setText("Connected");
break;
case STATE_CONNECTION_FAILED:
connectionStatusText.setText("Connection failed");
break;
case STATE_MESSAGE_RECEIVED:
byte[] readBuff = (byte[]) message.obj;
//String tempMsg = new String(readBuff, 0 , message.arg1);
String tempMsg = null;
try {
tempMsg = new String(readBuff, "ASCII");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
Toast.makeText(getApplicationContext(), String.valueOf(readBuff), Toast.LENGTH_SHORT).show();
connectionStatusText.setText(tempMsg);
break;
}
return true;
}
});
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
bluetoothEnablingIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
requestCodeForEnable = 1;
if(!myBluetoothAdapter.isEnabled()){
Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableIntent, REQUEST_ENABLE_BLUETOOTH);
}
findViewByIds();
implementListeners();
}
private void findViewByIds() {
pairedDevListView = findViewById(R.id.pairedDevicesListView);
bluetoothSwitch = findViewById(R.id.btSwitch);
connectionStatusText = findViewById(R.id.statusTextView);
rightEnSeekBar = findViewById(R.id.rightSeekBar);
leftEnSeekBar = findViewById(R.id.leftSeekBar);
someButton = findViewById(R.id.button2);
}
private void implementListeners() {
//BLUETOOTH ENABLING SWITCH
bluetoothSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if(b){
if(myBluetoothAdapter == null){
Toast.makeText(getApplicationContext(),"Bluetooth is not supported on this device", Toast.LENGTH_LONG).show();
}
else{
if(!myBluetoothAdapter.isEnabled()){
startActivityForResult(bluetoothEnablingIntent, requestCodeForEnable);
}
}
}
else{
if(myBluetoothAdapter.isEnabled()){
myBluetoothAdapter.disable();
Toast.makeText(getApplicationContext(), "Bluetooth is disabled", Toast.LENGTH_LONG).show();
}
}
}
});
//CLICKING ON DEVICE FROM PAIRED DEVICE LIST
pairedDevListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
ClientClass clientClass = new ClientClass(bluetoothPairedDevArray[i]);
clientClass.start();
connectionStatusText.setText("Connecting");
}
});
someButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String string = "1";
try {
sendReceive.write(string.getBytes("ASCII"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
});
}
//SHOWING PAIRED DEVICES WHEN SWITCH IS CHECKED
private void showPairedDevices() {
Set<BluetoothDevice> btDevices = myBluetoothAdapter.getBondedDevices();
bluetoothPairedDevArray = new BluetoothDevice[btDevices.size()];
String[] deviceNames = new String[btDevices.size()];
int index = 0;
if(btDevices.size() > 0){
for(BluetoothDevice device: btDevices){
deviceNames[index] = device.getName();
bluetoothPairedDevArray[index] = device;
index++;
}
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, deviceNames);
pairedDevListView.setAdapter(arrayAdapter);
}
}
//SHOWING POP UP MESSAGE
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
if(requestCode == requestCodeForEnable){
if(resultCode == RESULT_OK){
Toast.makeText(getApplicationContext(), "Bluetooth is enabled", Toast.LENGTH_LONG).show();
showPairedDevices();
}
else if(resultCode == RESULT_CANCELED){
Toast.makeText(getApplicationContext(), "Bluetooth enabling cancelled", Toast.LENGTH_LONG).show();
}
}
}
private class ClientClass extends Thread{
private BluetoothSocket socket;
private BluetoothDevice device;
public ClientClass(BluetoothDevice device1){
this.device = device1;
try {
socket = device.createInsecureRfcommSocketToServiceRecord(MY_UUID);
} catch (IOException e) {
e.printStackTrace();
}
}
public void run(){
myBluetoothAdapter.cancelDiscovery();
try {
socket.connect();
Message message = Message.obtain();
message.what = STATE_CONNECTED;
handler.sendMessage(message);
sendReceive = new SendReceive(socket);
sendReceive.start();
} catch (IOException e) {
e.printStackTrace();
Message message = Message.obtain();
message.what = STATE_CONNECTION_FAILED;
handler.sendMessage(message);
}
}
}
private class SendReceive extends Thread {
private final BluetoothSocket bluetoothSocket;
private final InputStream inputStream;
private final OutputStream outputStream;
public SendReceive(BluetoothSocket socket) {
bluetoothSocket = socket;
InputStream tempIn = null;
OutputStream tempOut = null;
try {
tempIn = bluetoothSocket.getInputStream();
tempOut = bluetoothSocket.getOutputStream();
} catch (IOException e) {
e.printStackTrace();
}
inputStream = tempIn;
outputStream = tempOut;
}
public void run() {
byte[] buffer = new byte[1024];
int bytes;
while (true) {
try {
bytes = inputStream.read(buffer);
handler.obtainMessage(STATE_MESSAGE_RECEIVED, bytes, -1, buffer).sendToTarget();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void write(byte[] bytes) {
try {
outputStream.write(bytes);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
My STM code:
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) {
switch (atoi(&Received)) {
case 0:
size = sprintf(Data, "STOP\n\r");
HAL_GPIO_WritePin(BLUE_LED_GPIO_Port, BLUE_LED_Pin, GPIO_PIN_RESET);
break;
case 1:
size = sprintf(Data, "START\n\r");
HAL_GPIO_WritePin(BLUE_LED_GPIO_Port, BLUE_LED_Pin, GPIO_PIN_SET);
break;
default:
size = sprintf(Data, "Odebrano nieznany znak: %c\n\r", Received);
break;
}
HAL_UART_Transmit_IT(&huart1, Data, size);
HAL_UART_Receive_IT(&huart1, &Received, 1);
HAL_GPIO_TogglePin(RED_LED_GPIO_Port, RED_LED_Pin);
}
So what i want to do basically is to scroll the scroll view as the client app sends the command. The method handleScrolling() works in the onCreate() but whenever i call it from the Thread, it doesn't scroll. I think that calling it from the Thread is the problem.
public class TeleprompterActivity extends Activity {
static ScrollView mScrollView;
TextView teleprompterTextView;
String message;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_teleprompter);
teleprompterTextView = findViewById(R.id.teleprompter_text_view);
teleprompterTextView.setText(MainActivity.contents);
mScrollView = findViewById(R.id.scroll);
Thread mThread = new Thread(new ServerClass());
mThread.start();
handleScrolling(3, true);
Log.e("TELE", "handleScrolling at start onCrete");
}
void handleScrolling(final int numLines, final boolean next) {
mScrollView.post(new Runnable() {
public void run() {
int currPosition = mScrollView.getScrollY();
int textSize = (int) teleprompterTextView.getTextSize();
if (next) {
int newPos = currPosition + (textSize * numLines);
mScrollView.smoothScrollTo(0, newPos);
}
if (!next) {
int newPos = currPosition - (textSize * numLines);
if (newPos < 0) {
newPos = 0;
}
mScrollView.scrollTo(0, newPos);
}
}
});
}
void handleMessage(String message) {
if (message.contains("NEXT:")) {
try {
int numLine = Integer.parseInt(message.substring(5));
handleScrolling(numLine, true);
return;
} catch (NumberFormatException nFE) {
nFE.printStackTrace();
Toast.makeText(TeleprompterActivity.this, "Number Format Exception", Toast.LENGTH_SHORT).show();
}
}
if (message.contains("BACK:")) {
try {
int numLine = Integer.parseInt(message.substring(5));
handleScrolling(numLine, false);
} catch (NumberFormatException nFE) {
nFE.printStackTrace();
Toast.makeText(TeleprompterActivity.this, "Number Format Exception", Toast.LENGTH_SHORT).show();
}
}
}
public class ServerClass implements Runnable {
ServerSocket serverSocket;
Socket socket;
DataInputStream dataInputStream;
String receivedData;
Handler handler = new Handler();
#Override
public void run() {
try {
serverSocket = new ServerSocket(8080);
Log.e("TELE", "WAITIN FOR CLIENT");
while (true) {
socket = serverSocket.accept();
dataInputStream = new DataInputStream(socket.getInputStream());
receivedData = dataInputStream.readUTF();
handler.post(new Runnable() {
#Override
public void run() {
message = receivedData;
handleMessage(message);
}
});
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
The handleScrolling() method works perfectly when called outside the Thread. Is it any problem of reference or something like that.
Thanks..
Never update UI components from a thread directly ,call Activity's runOnUiThread so you can use code construct like below:
public class TeleprompterActivity extends Activity {
static ScrollView mScrollView;
TextView teleprompterTextView;
String message;
Activity thisActivity;//<--- used for saving reference to this Activity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_teleprompter);
teleprompterTextView = findViewById(R.id.teleprompter_text_view);
teleprompterTextView.setText(MainActivity.contents);
mScrollView = findViewById(R.id.scroll);
thisActivity = this;//<---- saving a reference to this activity
Thread mThread = new Thread(new ServerClass());
mThread.start();
handleScrolling(3, true);
Log.e("TELE", "handleScrolling at start onCrete");
}
void handleScrolling(final int numLines, final boolean next) {
thisActivity.runOnUiThread(new Runnable()//<--used thisActivity.runOnUiThread here
{
public void run() {
int currPosition = mScrollView.getScrollY();
int textSize = (int) teleprompterTextView.getTextSize();
if (next) {
int newPos = currPosition + (textSize * numLines);
mScrollView.smoothScrollTo(0, newPos);
}
if (!next) {
int newPos = currPosition - (textSize * numLines);
if (newPos < 0) {
newPos = 0;
}
mScrollView.scrollTo(0, newPos);
}
}
});
}
I had this error on my VoiSip Application
E/AudioRecord: AudioFlinger could not create record track, status: -1
E/AudioGroup: cannot initialize audio device
This error occurs after I'm trying to make call to another sip address
Here have 2 java class(WalkieTalkieActivity.java & IncomingCallReceiver.java)
WalkieTalkieActivity.java
public class WalkieTalkieActivity extends Activity implements View.OnTouchListener {
public IncomingCallReceiver callReceiver;
public SipManager mSipManager = null;
public SipProfile mSipProfile = null;
public SipAudioCall call = null;
TextView tv;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_walkie_talkie);
ToggleButton pushToTalkButton = (ToggleButton) findViewById(R.id.pushToTalk);
pushToTalkButton.setOnTouchListener(this);
IntentFilter filter = new IntentFilter();
filter.addAction("android.SipDemo.INCOMING_CALL");
callReceiver = new IncomingCallReceiver();
this.registerReceiver(callReceiver, filter);
if (mSipManager == null) {
mSipManager = SipManager.newInstance(this);
}
tv = (TextView)findViewById(R.id.textView);
}
#Override
public void onStart() {
super.onStart();
// When we get back from the preference setting Activity, assume
// settings have changed, and re-login with new auth info.
initializeManager();
}
#Override
public void onDestroy() {
super.onDestroy();
if (call != null) {
call.close();
}
closeLocalProfile();
if (callReceiver != null) {
this.unregisterReceiver(callReceiver);
}
}
public void closeLocalProfile() {
if (mSipManager == null) {
return;
}
try {
if (mSipProfile != null) {
mSipManager.close(mSipProfile.getUriString());
}
} catch (Exception ee) {
Log.d("onDestroy", "Failed to close local profile.", ee);
}
}
public void initializeManager() {
if(mSipManager == null) {
mSipManager = SipManager.newInstance(this);
}
initializeLocalProfile();
}
private void initializeLocalProfile() {
String domain = "mydomain";
String username = "myusername";
String password = "mypassword";
try {
SipProfile.Builder builder = new SipProfile.Builder(username, domain);
builder.setPassword(password);
mSipProfile = builder.build();
if (mSipProfile == null){
Log.e("error cukimai", "null");
}else{
Log.e("error cukimai", "not null");
}
Intent i = new Intent();
i.setAction("android.SipDemo.INCOMING_CALL");
PendingIntent pi = PendingIntent.getBroadcast(this, 0, i, Intent.FILL_IN_DATA);
mSipManager.open(mSipProfile, pi, null);
mSipManager.setRegistrationListener(mSipProfile.getUriString(), new SipRegistrationListener() {
public void onRegistering(String localProfileUri) {
updateStatus("Registering with SIP Server...");
Log.e("process","Registering with SIP Server...");
}
public void onRegistrationDone(String localProfileUri, long expiryTime) {
updateStatus("Ready");
Log.e("process","ready");
}
public void onRegistrationFailed(String localProfileUri, int errorCode,
String errorMessage) {
updateStatus("Registration failed. Please check settings.");
Log.e("process","Registration failed. Please check settings.");
}
});
Log.e("process","stop");
} catch (SipException e) {
e.printStackTrace();
Log.e("error cukimai", "cuk cuk");
} catch (ParseException e) {
e.printStackTrace();
Log.e("error cukimai", "cuk");
}
}
public void updateStatus(final String st){
this.runOnUiThread(new Runnable() {
public void run() {
tv.setText(st);
}
});
}
public void updateStatus(SipAudioCall call) {
String useName = call.getPeerProfile().getDisplayName();
if(useName == null) {
useName = call.getPeerProfile().getUserName();
}
updateStatus(useName + "#" + call.getPeerProfile().getSipDomain());
}
public void callAct(View view) {
Toast.makeText(this, "about to make call", Toast.LENGTH_LONG).show();
makeCall();
}
public void makeCall(){
try {
SipAudioCall.Listener listener = new SipAudioCall.Listener() {
// Much of the client's interaction with the SIP Stack will
// happen via listeners. Even making an outgoing call, don't
// forget to set up a listener to set things up once the call is established.
#Override
public void onCallEstablished(SipAudioCall call) {
call.startAudio();
call.setSpeakerMode(true);
call.toggleMute();
updateStatus(call);
}
#Override
public void onCallEnded(SipAudioCall call) {
updateStatus("Ready.");
}
};
call = mSipManager.makeAudioCall(mSipProfile.getUriString(), "sip:destination#domain", listener, 30);
Log.e("make call", "true");
start();
}catch (Exception e){
Log.i("error", "Error when trying to close manager.", e);
if (mSipProfile != null) {
try {
mSipManager.close(mSipProfile.getUriString());
} catch (Exception ee) {
Log.i("error", "Error when trying to close manager.", ee);
ee.printStackTrace();
}
}
if (call != null) {
call.close();
}
}
}
#Override
public boolean onTouch(View view, MotionEvent event) {
if (call == null) {
return false;
} else if (event.getAction() == MotionEvent.ACTION_DOWN && call != null && call.isMuted()) {
call.toggleMute();
} else if (event.getAction() == MotionEvent.ACTION_UP && !call.isMuted()) {
call.toggleMute();
}
return false;
}
final MediaRecorder recorder = new MediaRecorder();
final String path;
/**
* Creates a new audio recording at the given path (relative to root of SD card).
*/
public WalkieTalkieActivity(String path) {
this.path = sanitizePath(path);
}
private String sanitizePath(String path) {
if (!path.startsWith("/")) {
path = "/" + path;
}
if (!path.contains(".")) {
path += ".3gp";
}
return Environment.getExternalStorageDirectory().getAbsolutePath() + path;
}
/**
* Starts a new recording.
*/
public void start() throws IOException {
String state = android.os.Environment.getExternalStorageState();
if(!state.equals(android.os.Environment.MEDIA_MOUNTED)) {
throw new IOException("SD Card is not mounted. It is " + state + ".");
}
// make sure the directory we plan to store the recording in exists
File directory = new File(path).getParentFile();
if (!directory.exists() && !directory.mkdirs()) {
throw new IOException("Path to file could not be created.");
}
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(path);
recorder.prepare();
recorder.start();
}
/**
* Stops a recording that has been previously started.
*/
public void stop() throws IOException {
recorder.stop();
recorder.release();
}
}
IncomingCallReceiver.java
public class IncomingCallReceiver extends BroadcastReceiver {
/**
* Processes the incoming call, answers it, and hands it over to the
* WalkieTalkieActivity.
* #param context The context under which the receiver is running.
* #param intent The intent being received.
*/
#Override
public void onReceive(Context context, Intent intent) {
SipAudioCall incomingCall = null;
try {
SipAudioCall.Listener listener = new SipAudioCall.Listener() {
#Override
public void onRinging(SipAudioCall call, SipProfile caller) {
try {
call.answerCall(30);
} catch (Exception e) {
e.printStackTrace();
}
}
};
WalkieTalkieActivity wtActivity = (WalkieTalkieActivity) context;
incomingCall = wtActivity.mSipManager.takeAudioCall(intent, listener);
incomingCall.answerCall(30);
incomingCall.startAudio();
incomingCall.setSpeakerMode(true);
if(incomingCall.isMuted()) {
incomingCall.toggleMute();
}
wtActivity.call = incomingCall;
wtActivity.updateStatus(incomingCall);
} catch (Exception e) {
if (incomingCall != null) {
incomingCall.close();
}
}
}
}
I'm really new on Sip and Voip Implementing in Android Studio. I got this code from google source code.
I believe this error occurs because of the use of hardware (audio). However I have been searching on google for almost 1 week and not giving results. Can someone help me?
I had same problem but when i changed targetSdkVersion to 12 in build.gradel its fixed.
and this answer helped me to fixed problem .
You must choose the BufferSize when you want to record or call in app. For example:
int bufferSize = AudioRecord.getMinBufferSize(rate, channelConfig, audioFormat);
if (bufferSize > 0 && bufferSize <= 256){
bufferSize = 256;
}else if (bufferSize > 256 && bufferSize <= 512){
bufferSize = 512;
}else if (bufferSize > 512 && bufferSize <= 1024){
bufferSize = 1024;
}else if (bufferSize > 1024 && bufferSize <= 2048){
bufferSize = 2048;
}else if (bufferSize > 2048 && bufferSize <= 4096){
bufferSize = 4096;
}else if (bufferSize > 4096 && bufferSize <= 8192){
bufferSize = 8192;
}else if (bufferSize > 8192 && bufferSize <= 16384){
bufferSize = 16384;
}else{
bufferSize = AudioRecord.getMinBufferSize(rate, channelConfig, audioFormat);
}
I want to send real time accelerometer data from my android phone to my laptop, so that I can do some further analysis using that data, but I am facing some challenges in doing so...I am not able to persist connection at the android side and getting
W/BluetoothAdapter: getBluetoothService() called with no BluetoothManagerCallback
03-20 23:30:19.881 10050-13445/com.accel.bluetoothconnect W/System.err: java.io.IOException: read failed, socket might closed or timeout, read ret: -1
03-20 23:30:19.881 10050-13445/com.accel.bluetoothconnect W/System.err: at android.bluetooth.BluetoothSocket.readAll(BluetoothSocket.java:1016)
03-20 23:30:19.881 10050-13445/com.accel.bluetoothconnect W/System.err: at android.bluetooth.BluetoothSocket.waitSocketSignal(BluetoothSocket.java:973)
03-20 23:30:19.881 10050-13445/com.accel.bluetoothconnect W/System.err: at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:662)
03-20 23:30:19.881 10050-13445/com.accel.bluetoothconnect W/System.err: at com.accel.bluetoothconnect.MainActivity$ConnectingThread.run(MainActivity.java:446)
03-20 23:30:19.951 10050-10050/com.accel.bluetoothconnect W/Accel Value(X)->: 0.0
03-20 23:30:19.951 10050-10050/com.accel.bluetoothconnect W/Accel Value(Y)->: 0.0
03-20 23:30:19.951 10050-10050/com.accel.bluetoothconnect W/Accel Value(Z)->: 10.289082
03-20 23:30:20.151 10050-10050/com.accel.bluetoothconnect W/Accel Value(X)->: 0.0
03-20 23:30:20.151 10050-10050/com.accel.bluetoothconnect W/Accel Value(Y)->: 0.0
03-20 23:30:20.151 10050-10050/com.accel.bluetoothconnect W/Accel Value(Z)->: 10.271125
03-20 23:30:20.351 10050-10050/com.accel.bluetoothconnect W/Accel Value(X)->: 0.0
03-20 23:30:20.351 10050-10050/com.accel.bluetoothconnect W/Accel Value(Y)->: 0.0
03-20 23:30:20.351 10050-10050/com.accel.bluetoothconnect W/Accel Value(Z)->: 10.289082
whereas, on the laptop side where bluecove code is running, it is able to connect and the thread waits for the data.
BlueCove version 2.1.1-SNAPSHOT on winsock
0000110100001000800000805f9b34fb
waiting for connection...
waiting for connection...
waiting for input
finish process
Codes that I am using,
For Android-
public class MainActivity extends Activity implements SensorEventListener {
private final static UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb");
private static final String TAG="BluetoothConnector";
private static final int REQUEST_ENABLE_BT = 1;
private static final int DISCOVERABLE_BT_REQUEST_CODE = 2;
private static final int DISCOVERABLE_DURATION = 300;
public static final int EXIT_CMD = -1;
int conn_flag = 0;
private Button onBtn;
private Button offBtn;
private Button listBtn;
private Button findBtn;
private TextView text;
private BluetoothAdapter myBluetoothAdapter;
private Set<BluetoothDevice> pairedDevices;
private ListView myListView;
private ArrayAdapter<String> BTArrayAdapter;
BluetoothDevice bluetoothDevice;
private float lastX, lastY, lastZ;
private SensorManager sensorManager;
private Sensor accelerometer;
private float deltaXMax = 0;
private float deltaYMax = 0;
private float deltaZMax = 0;
private float deltaX = 0;
private float deltaY = 0;
private float deltaZ = 0;
private float vibrateThreshold = 0;
private TextView currentX, currentY, currentZ, maxX, maxY, maxZ;
public Vibrator v;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// take an instance of BluetoothAdapter - Bluetooth radio
myBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if(myBluetoothAdapter == null) {
onBtn.setEnabled(false);
offBtn.setEnabled(false);
listBtn.setEnabled(false);
findBtn.setEnabled(false);
text.setText("Status: not supported");
Toast.makeText(getApplicationContext(),"Your device does not support Bluetooth",
Toast.LENGTH_LONG).show();
} else {
text = (TextView) findViewById(R.id.text);
onBtn = (Button)findViewById(R.id.turnOn);
onBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
on(v);
}
});
offBtn = (Button)findViewById(R.id.turnOff);
offBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
off(v);
}
});
listBtn = (Button)findViewById(R.id.paired);
listBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
list(v);
}
});
findBtn = (Button)findViewById(R.id.search);
findBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
find(v);
}
});
myListView = (ListView)findViewById(R.id.listView1);
// create the arrayAdapter that contains the BTDevices, and set it to the ListView
BTArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);
myListView.setAdapter(BTArrayAdapter);
myListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// ListView Clicked item value
String itemValue = (String) myListView.getItemAtPosition(position);
String MAC = itemValue.substring(itemValue.length() - 17);
bluetoothDevice = myBluetoothAdapter.getRemoteDevice(MAC);
// Initiate a connection request in a separate thread
ConnectingThread t = new ConnectingThread(bluetoothDevice);
t.start();
}
});
}
sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
if (sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER) != null) {
// success! we have an accelerometer
accelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
sensorManager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_NORMAL);
vibrateThreshold = accelerometer.getMaximumRange() / 2;
} else {
// fai! we dont have an accelerometer!
}
//initialize vibration
v = (Vibrator) this.getSystemService(Context.VIBRATOR_SERVICE);
}
public void on(View view){
if (!myBluetoothAdapter.isEnabled()) {
Intent turnOnIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(turnOnIntent, REQUEST_ENABLE_BT);
Toast.makeText(getApplicationContext(),"Bluetooth turned on" ,
Toast.LENGTH_LONG).show();
}
else{
Toast.makeText(getApplicationContext(),"Bluetooth is already on",
Toast.LENGTH_LONG).show();
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
if(requestCode == REQUEST_ENABLE_BT){
if(myBluetoothAdapter.isEnabled()) {
text.setText("Status: Enabled");
Toast.makeText(getApplicationContext(), "Ha! Bluetooth is now enabled." +
"\n" + "Scanning for remote Bluetooth devices...",
Toast.LENGTH_SHORT).show();
// To discover remote Bluetooth devices
discoverDevices();
// Make local device discoverable by other devices
makeDiscoverable();
// Start a thread to create a server socket to listen
// for connection request
ListeningThread t = new ListeningThread();
t.start();
} else {
text.setText("Status: Disabled");
}
}else if (requestCode == DISCOVERABLE_BT_REQUEST_CODE){
if (resultCode == DISCOVERABLE_DURATION){
Toast.makeText(getApplicationContext(), "Your device is now discoverable by other devices for " +
DISCOVERABLE_DURATION + " seconds",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "Fail to enable discoverability on your device.",
Toast.LENGTH_SHORT).show();
}
}
}
protected void discoverDevices(){
// To scan for remote Bluetooth devices
if (myBluetoothAdapter.startDiscovery()) {
Toast.makeText(getApplicationContext(), "Discovering other bluetooth devices...",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "Discovery failed to start.",
Toast.LENGTH_SHORT).show();
}
}
protected void makeDiscoverable(){
// Make local device discoverable
Intent discoverableIntent = new
Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, DISCOVERABLE_DURATION);
startActivityForResult(discoverableIntent, DISCOVERABLE_BT_REQUEST_CODE);
}
public void list(View view){
// get paired devices
pairedDevices = myBluetoothAdapter.getBondedDevices();
// put it's one to the adapter
for(BluetoothDevice device : pairedDevices)
BTArrayAdapter.add(device.getName()+ "\n" + device.getAddress());
Toast.makeText(getApplicationContext(),"Show Paired Devices",
Toast.LENGTH_SHORT).show();
}
final BroadcastReceiver bReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
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);
// add the name and the MAC address of the object to the arrayAdapter
BTArrayAdapter.add(device.getName() + "\n" + device.getAddress());
BTArrayAdapter.notifyDataSetChanged();
}
else if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) {
conn_flag=1;
}
}
};
public void find(View view) {
if (myBluetoothAdapter.isDiscovering()) {
// the button is pressed when it discovers, so cancel the discovery
myBluetoothAdapter.cancelDiscovery();
}
else {
BTArrayAdapter.clear();
myBluetoothAdapter.startDiscovery();
registerReceiver(bReceiver, new IntentFilter(BluetoothDevice.ACTION_FOUND));
}
}
public void off(View view){
myBluetoothAdapter.disable();
text.setText("Status: Disconnected");
Toast.makeText(getApplicationContext(),"Bluetooth turned off",
Toast.LENGTH_LONG).show();
}
#Override
protected void onResume() {
super.onResume();
sensorManager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_NORMAL);
// Register the BroadcastReceiver for ACTION_FOUND
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
this.registerReceiver(bReceiver, filter);
}
#Override
protected void onPause() {
super.onPause();
sensorManager.unregisterListener(this);
this.unregisterReceiver(bReceiver);
}
#Override
public void onSensorChanged(SensorEvent event) {
Log.w("Accel Value(X)->", String.valueOf(deltaX));
Log.w("Accel Value(Y)->", String.valueOf(deltaY));
Log.w("Accel Value(Z)->", String.valueOf(deltaZ));
if(conn_flag == 1) {
Log.w("Data Transfer", "Finally transferring data");
ConnectingThread ct = new ConnectingThread(bluetoothDevice);
ct.write((int) deltaX);
}
// get the change of the x,y,z values of the accelerometer
deltaX = Math.abs(lastX - event.values[0]);
deltaY = Math.abs(lastY - event.values[1]);
deltaZ = Math.abs(lastZ - event.values[2]);
// if the change is below 2, it is just plain noise
if (deltaX < 2)
deltaX = 0;
if (deltaY < 2)
deltaY = 0;
if ((deltaZ > vibrateThreshold) || (deltaY > vibrateThreshold) || (deltaZ > vibrateThreshold)) {
v.vibrate(50);
}
}
#Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
private class ListeningThread extends Thread {
private final BluetoothServerSocket bluetoothServerSocket;
public ListeningThread() {
BluetoothServerSocket temp = null;
try {
temp = myBluetoothAdapter.listenUsingRfcommWithServiceRecord(getString(R.string.app_name), uuid);
} catch (IOException e) {
e.printStackTrace();
}
bluetoothServerSocket = temp;
}
public void run() {
BluetoothSocket bluetoothSocket;
// This will block while listening until a BluetoothSocket is returned
// or an exception occurs
while (true) {
try {
bluetoothSocket = bluetoothServerSocket.accept();
} catch (IOException e) {
break;
}
// If a connection is accepted
if (bluetoothSocket != null) {
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(getApplicationContext(), "A connection has been accepted.",
Toast.LENGTH_SHORT).show();
}
});
// Code to manage the connection in a separate thread
/*
manageBluetoothConnection(bluetoothSocket);
*/
try {
bluetoothServerSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
break;
}
}
}
// Cancel the listening socket and terminate the thread
public void cancel() {
try {
bluetoothServerSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private class ConnectingThread extends Thread {
private BluetoothSocket bluetoothSocket;
private final BluetoothDevice bluetoothDevice;
private final InputStream mmInStream;
private final OutputStream mmOutStream;
public ConnectingThread(BluetoothDevice device) {
BluetoothSocket temp = null;
bluetoothDevice = device;
// Get a BluetoothSocket to connect with the given BluetoothDevice
try {
temp = bluetoothDevice.createRfcommSocketToServiceRecord(uuid);
} catch (IOException e) {
e.printStackTrace();
}
bluetoothSocket = temp;
InputStream tmpIn = null;
OutputStream tmpOut = null;
// Get the BluetoothSocket input and output streams
try {
tmpIn = bluetoothSocket.getInputStream();
tmpOut = bluetoothSocket.getOutputStream();
} catch (IOException e) {
Log.e(TAG, "temp sockets not created", e);
}
mmInStream = tmpIn;
mmOutStream = tmpOut;
}
public void run() {
myBluetoothAdapter.cancelDiscovery();
try {
// This will block until it succeeds in connecting to the device
// through the bluetoothSocket or throws an exception
bluetoothSocket.connect();
} catch (IOException connectException) {
connectException.printStackTrace();
try {
Log.e("","trying fallback...");
bluetoothSocket =(BluetoothSocket) bluetoothDevice.getClass().getMethod("createRfcommSocket", new Class[] {int.class}).invoke(bluetoothDevice,1);
bluetoothSocket.connect();
Log.e("","Connected");
}
catch (Exception e2) {
Log.e("", "Couldn't establish Bluetooth connection!");
}
}
}
public void write(int out) {
try {
mmOutStream.write(out);
} catch (IOException e) {
Log.e(TAG, "Exception during write", e);
}
}
// Cancel an open connection and terminate the thread
public void cancel() {
try {
mmOutStream.write(EXIT_CMD);
bluetoothSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}}
Android manifest-
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-permission android:name="android.permission.VIBRATE" />
For fallback code, I referred to- getBluetoothService() called with no BluetoothManagerCallback for Android Nexus 5
and laptop side (bluecove)-
WaitThread.java-
public class WaitThread implements Runnable{
/** Constructor */
public WaitThread() {
}
#Override
public void run() {
waitForConnection();
}
/** Waiting for connection from devices */
private void waitForConnection() {
// retrieve the local Bluetooth device object
LocalDevice local = null;
StreamConnectionNotifier notifier;
StreamConnection connection = null;
// setup the server to listen for connection
try {
local = LocalDevice.getLocalDevice();
local.setDiscoverable(DiscoveryAgent.GIAC);
//UUID uuid = new UUID("04c6093b00001000800000805f9b34fb", false);
UUID uuid = new UUID("0000110100001000800000805f9b34fb", false);
System.out.println(uuid.toString());
String url = "btspp://localhost:" + uuid.toString() + ";name=RemoteBluetooth";
notifier = (StreamConnectionNotifier)Connector.open(url);
} catch (BluetoothStateException e) {
System.out.println("Bluetooth is not turned on.");
e.printStackTrace();
return;
} catch (IOException e) {
e.printStackTrace();
return;
}
// waiting for connection
while(true) {
try {
System.out.println("waiting for connection...");
connection = notifier.acceptAndOpen();
Thread processThread = new Thread(new ProcessConnectionThread(connection));
processThread.start();
} catch (Exception e) {
e.printStackTrace();
return;
}
}
}
}
and ProcessConnectionThread.java-
public class ProcessConnectionThread implements Runnable{
private StreamConnection mConnection;
// Constant that indicate command from devices
private static final int EXIT_CMD = -1;
private static final int KEY_RIGHT = 1;
private static final int KEY_LEFT = 2;
public ProcessConnectionThread(StreamConnection connection)
{
mConnection = connection;
}
#Override
public void run() {
try {
// prepare to receive data
InputStream inputStream = mConnection.openInputStream();
System.out.println("waiting for input");
while (true) {
int command = inputStream.read();
if (command == EXIT_CMD)
{
System.out.println("finish process");
break;
}
processCommand(command);
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Process the command from client
* #param command the command code
*/
private void processCommand(int command) {
try {
System.out.println("Value sent is: "+command);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Right now i'm just trying to pass x axis data that too in int (just for the moment). Please help me in getting it resolved. Also, this is my first android app and i have tried to connect pieces from various websites, so requesting everyone to please go easy with me.
I am doing a project involving Android and Arduino. My project has two LEDs, with one controlling the brightness and the other just on and off the led. I'm having problems in the arduino part for compiling my brightness program with the TurnOn/OFF led program. Any suggestions on what I should do?
Android Program for brightness:
sr.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener(){
#Override
public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
srValue = (byte) arg1;
}
#Override
public void onStartTrackingTouch(SeekBar arg0) {
}
#Override
public void onStopTrackingTouch(SeekBar arg0) {
String temp = "r";
byte bytes1[] = temp.getBytes();
try {
outStream.write(bytes1);
} catch (IOException e) {
e.printStackTrace();
}
try {
outStream.write(srValue);
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
Android program for ON/OFF Led:
tg2.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
if((tg2.isChecked()))
{
System.out.println("checked");
tv2.setBackgroundColor(0xFF00FF00);
sendData("1");
}
else
{
System.out.println("Unchecked");
tv2.setBackgroundColor(0xfff00000);
sendData("4");
}
}
});
Arduino Program for brightness:
byte packet[2];
int pIndex;
int rPin = 9;
byte rValue = 0;
void setup() {
Serial.begin(9600);
pinMode(rPin, OUTPUT);
analogWrite(rPin, 0);
}
void loop() {
// see if there's incoming serial data:
if (Serial.available() > 0) {
packet[pIndex++] = Serial.read();
}
if(pIndex >= 2){
switch(packet[0]){
case 't':
rValue = packet[0];
break;
case 'y':
rValue = packet[3];
break;
case 'r':
rValue = packet[1];
break;
default:
;
}
analogWrite(rPin, rValue); // 0 - 255
pIndex = 0;
}
}
Arduino Program for Turn ON/OFF:
if (state == '1') // ON LED 2
{
analogWrite(ledPin2, 255);
}
else if(state == '4')
{
analogWrite(ledPin2,0);
}