Comments (10)
You got null pointer exception because you not setup and started bluetooth service.
mBluetoothSPP = new BluetoothSPP(getContext());
if (!mBluetoothSPP.isBluetoothAvailable()) {
//oops, don't have bluetooth
return;
}
if (!mBluetoothSPP.isBluetoothEnabled()) {
//bluetooth is turned off, let's try to turn it on
mBluetoothSPP.enable();
}
mBluetoothSPP.setupService(); // setup bluetooth service
mBluetoothSPP.startService(true); // start bluetooth service
from bluetoothspplibrary.
thx :)
from bluetoothspplibrary.
@ryazmin
I am using below code but I didn't understand purpose of send button and I am getting error nullpointerexception
package in.harman.android_barcode_scanner;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
import app.akexorcist.bluetotohspp.library.BluetoothSPP;
import app.akexorcist.bluetotohspp.library.BluetoothSPP.OnDataReceivedListener;
import app.akexorcist.bluetotohspp.library.BluetoothState;
import app.akexorcist.bluetotohspp.library.DeviceList;
public class BluetoothConnectionActivity extends Activity {
BluetoothSPP bt;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_devicelist);
bt = new BluetoothSPP(getApplicationContext());
if(!bt.isBluetoothAvailable()) {
Toast.makeText(getApplicationContext()
, "Bluetooth is not available"
, Toast.LENGTH_SHORT).show();
finish();
}
bt.setOnDataReceivedListener(new OnDataReceivedListener() {
public void onDataReceived(byte[] data, String message) {
Log.i("Check", "Length : " + data.length);
Log.i("Check", "Message : " + message);
}
});
Button btnConnect = (Button)findViewById(R.id.btnConnect);
btnConnect.setOnClickListener(new OnClickListener(){
public void onClick(View v){
if(bt.getServiceState() == BluetoothState.STATE_CONNECTED) {
bt.disconnect();
} else {
Intent intent = new Intent(BluetoothConnectionActivity.this, DeviceList.class);
intent.putExtra("bluetooth_devices", "Bluetooth devices");
intent.putExtra("no_devices_found", "No device");
intent.putExtra("scanning", "Scanning");
intent.putExtra("scan_for_devices", "Search");
intent.putExtra("select_device", "Select");
intent.putExtra("layout_list", R.layout.device_layout_list);
intent.putExtra("layout_text", R.layout.device_layout_text);
startActivityForResult(intent, BluetoothState.REQUEST_CONNECT_DEVICE);
}
}
});
}
public void onDestroy() {
super.onDestroy();
bt.stopService();
}
public void onStart() {
super.onStart();
if(!bt.isBluetoothEnabled()) {
bt.enable();
} else {
if(!bt.isServiceAvailable()) {
bt.setupService();
bt.startService(BluetoothState.DEVICE_ANDROID);
//setup();
}
}
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == BluetoothState.REQUEST_CONNECT_DEVICE) {
if(resultCode == Activity.RESULT_OK)
Toast.makeText(getApplicationContext(),"Connected",Toast.LENGTH_LONG).show();
bt.connect(data);
} else if(requestCode == BluetoothState.REQUEST_ENABLE_BT) {
if(resultCode == Activity.RESULT_OK) {
bt.setupService();
Toast.makeText(getApplicationContext(),"Enable_BT",Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext()
, "Bluetooth was not enabled."
, Toast.LENGTH_SHORT).show();
finish();
}
}
}
public void setup() {
Button btnSend = (Button)findViewById(R.id.btnSend);
btnSend.setOnClickListener(new OnClickListener(){
public void onClick(View v){
bt.send("Text", true);
}
});
}
}
from bluetoothspplibrary.
@vidyajejurkar send button will send "Text" to connected device over bluetooth. If you getting NPE then something wrong, stack trace will be helpful.
from bluetoothspplibrary.
@ryazmin Thanks for Reply :-)
I have one Question. I am using this library for Connection but I think they used BluetoothChat as it is which I don't want to do. instead of Chatting I want to send my app data like table to device which is supporting BluetoothSpp.
So how to modified it?
Need help.
from bluetoothspplibrary.
@vidyajejurkar I guess it's a question about how you representing your data on one end and how you interpreted it on another end.
Can you represent your data as json, for example? If so, just put your data to json, then take a string representation of json and send this string over bluetooth. On another end you will get this string, take it and construct the json from it. Now you can extract your data from json.
from bluetoothspplibrary.
Ok @ryazmin N I am facing one problem. sometime device gets connected sometime don't.
D/ViewRootImpl: ViewPostImeInputStage ACTION_DOWN
I/Timeline: Timeline: Activity_launch_request id:in.harman.android_barcode_scanner time:106607876
D/DisplayManager: DisplayManager()
I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@b37ae56 time:106608009
D/ViewRootImpl: ViewPostImeInputStage ACTION_DOWN
D/BluetoothSPP: doDiscovery()
D/ViewRootImpl: ViewPostImeInputStage ACTION_DOWN
D/BluetoothSPP: doDiscovery()
D/BluetoothSocket: close() in, this: android.bluetooth.BluetoothSocket@314f881e, channel: -1, state: CLOSED
D/ViewRootImpl: ViewPostImeInputStage ACTION_DOWN
D/Bluetooth Service: setState() 1 -> 2
D/BluetoothUtils: isSocketAllowedBySecurityPolicy start : device null
D/BluetoothSocket: connect(): myUserId = 0
W/BluetoothAdapter: getBluetoothService() called with no BluetoothManagerCallback
D/BluetoothSocket: connect(), SocketState: INIT, mPfd: {ParcelFileDescriptor: FileDescriptor[44]}
D/OpenGLRenderer: endAllStagingAnimators on 0xb8001148 (ListView) with handle 0xb7fcabc8
I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@2db363c8 time:106618111
D/BluetoothSocket: close() in, this: android.bluetooth.BluetoothSocket@2f12575c, channel: -1, state: INIT
D/BluetoothSocket: close() this: android.bluetooth.BluetoothSocket@2f12575c, channel: -1, mSocketIS: android.net.LocalSocketImpl$SocketInputStream@e956a65, mSocketOS: android.net.LocalSocketImpl$SocketOutputStream@2b27433amSocket: android.net.LocalSocket@f0f7beb impl:android.net.LocalSocketImpl@241b8d48 fd:FileDescriptor[44]
D/BluetoothSocket: Closing mSocket: android.net.LocalSocket@f0f7beb impl:android.net.LocalSocketImpl@241b8d48 fd:FileDescriptor[44]
D/BluetoothSocket: close() in, this: android.bluetooth.BluetoothSocket@2f12575c, channel: -1, state: CLOSED
D/Bluetooth Service: setState() 2 -> 1
from bluetoothspplibrary.
You got null pointer exception because you not setup and started bluetooth service.
mBluetoothSPP = new BluetoothSPP(getContext());
if (!mBluetoothSPP.isBluetoothAvailable()) {
//oops, don't have bluetooth
return;
}
if (!mBluetoothSPP.isBluetoothEnabled()) {
//bluetooth is turned off, let's try to turn it on
mBluetoothSPP.enable();
}
mBluetoothSPP.setupService(); // setup bluetooth service
mBluetoothSPP.startService(true); // start bluetooth service
Please include mBluetoothSPP.setupService(); in readme section also
from bluetoothspplibrary.
I am sending data from arduino but I didnt get any data
bt.setOnDataReceivedListener(new BluetoothSPP.OnDataReceivedListener() {
@OverRide
public void onDataReceived(byte[] data, String message) {
mTextView.setText(message);
Toast.makeText(getApplicationContext(), "Message is :"+message, Toast.LENGTH_SHORT).show();
}
});
ITS NOT WORKING
from bluetoothspplibrary.
Thanks ,it works .
from bluetoothspplibrary.
Related Issues (20)
- 이 블루투스 라이브러리를 사용하려는 한국인들을 위해 HOT 1
- transfer intent
- take multiple masseges on recived data
- Bt
- Context HOT 1
- NullPointerException at app.akexorcist.bluetotohspp.library.BluetoothService$AcceptThread.run (BluetoothService.java:2)
- Not scanning or discoverying nearby bluetooth devices. (startActivityForResult) HOT 2
- Prematurely terminating data packets with byte values of 10 (LF) and 13 (CR) HOT 2
- Can't read data from a BT barcode reader
- Activity crashed A/libc: Fatal signal 11 (SIGSEGV), code 1 HOT 1
- thank u
- getBluetoothService() called with no BluetoothManagerCallback HOT 1
- targetSdkVersion < 4
- the package name is spelled incorrectly...
- ConnectThread NullPointerException when the BT has been manually disabled
- How to make own Reciver
- bt.setupService()
- Fixing send function HOT 1
- How do you solve the issue of broken packets/messages? HOT 1
- Question: Will this library give me serial port in PC when my android device connected to PC using BT?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from bluetoothspplibrary.