Giter VIP home page Giter VIP logo

Comments (10)

ryazmin avatar ryazmin commented on August 15, 2024 1

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.

sakulstra avatar sakulstra commented on August 15, 2024

thx :)

from bluetoothspplibrary.

vidyajejurkar avatar vidyajejurkar commented on August 15, 2024

@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.

ryazmin avatar ryazmin commented on August 15, 2024

@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.

vidyajejurkar avatar vidyajejurkar commented on August 15, 2024

@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.

ryazmin avatar ryazmin commented on August 15, 2024

@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.

vidyajejurkar avatar vidyajejurkar commented on August 15, 2024

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.

ciestowp avatar ciestowp commented on August 15, 2024

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.

goherhussain avatar goherhussain commented on August 15, 2024

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.

hetthummar avatar hetthummar commented on August 15, 2024

Thanks ,it works .

from bluetoothspplibrary.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.